简体   繁体   中英

Gsub function replacing values with 3rd argument not 2nd

On R, I have a dataframe (overs) with a column ('Overs.Bowled') in characters, sometimes like "44.0" and sometimes as "-". I have used this code try to convert every "-" to "0":

overscon <- overs %>%
mutate('Overs.Bowled' = gsub('-','0','Overs.Bowled'))

This just turns every value in the entire column to 'Overs.Bowled', even the ones like "44.0". Please can you help me to convert every value in this column into a format where I can convert it to numeric type?

We don't need the quote for the column name in mutate . If we quote, it is evalluating literally as the string instead of the value in the column. The lhs of assigment ( = ) quote is also not needed, but it wouldn't have any issue with quote either

library(dplyr)
overs %>%
        mutate('Overs.Bowled' = gsub('-','0',Overs.Bowled))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM