I have a string variable containing the alphabet [az], space [] and apostrophe ['], for example. x <- "a'b c" I want to replace the apostrophe ['] with an empty [] and replace the space [] with an underscore [_].
x <- gsub("'", "", x) x <- gsub(" ", "_", x)
It works absolutely, but when I have a lot of conditions, the code gets ugly. So I want to use chartr() , but chartr() cannot handle space, for example.
x <- chartr("' ", "_", x) #Error in chartr("' ", "_", "a'b c") : 'old' is longer than 'new'
Is there any way to solve this problem? thanks!
string r gsub
Eric Chang
source share