Apply function to one column only

Hi

I have this df

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

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)
  df
2 Likes

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.