Apply function to one column only


I have this df

df <- tribble(~Name, ~Color,
              'car', 'green',
              'ship', 'red',
              'bus', 'green',

and I have this function

gsub( pattern, "", x )

How can I apply the gsub function to column Color only? I tried

df %>% gsub("bl","",df$Color)

but I get error.

Warning message:
In gsub(., "bl", "", df$Color) :
argument 'pattern' has length > 1 and only the first element will be used

Please help.

in dplyr style

df %>% mutate(newcol = gsub("bl","",Color)

in base style

  df$newcol <-  gsub("bl","",df$Color)

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.