Reputation:
I have the this data frame:
df=data.frame("fg"=c("bv65","bv89"),"gh"=c(87,21))
and I need to delete the substring "bv" in the column "fg". I did it this way:
sapply(df$fg,FUN=gsub(pattern="bv",replacement = "",x=df$fg))
but I get this error:
Error in match.fun(FUN) : 'gsub(pattern = "bv", replacement = "", x = df$fg)' is not a function, character or symbol
Why?
Upvotes: 0
Views: 132
Reputation: 33488
Ronak's solution is the way to go since gsub()
/sub()
can deal with vectors. But in case you wanted to know what was missing from your current approach:
sapply(df$fg, FUN = function(x) sub(pattern="bv", replacement="", x))
# Or simply
sapply(df$fg, sub, pattern="bv", replacement="")
Upvotes: 0
Reputation: 388982
sub
/gsub
are vectorized. You can use them for all the values in the column. Perhaps, you might also want to convert the values to numeric.
df$fg <- as.numeric(sub('bv', '', df$fg))
df
# fg gh
#1 65 87
#2 89 21
Upvotes: 1