Hi,
Try:
?string1<- "Red, Romeo, Calf"
?string2<- "Red, Rome, Ralf"
?string3<- "China, Japan, USA"
?gsub("R[[:alpha:]]{1,}","MM",string3)
#[1] "China, Japan, USA"
gsub("R[[:alpha:]]{1,}","MM",string2)
#[1] "MM, MM, MM"
?gsub("R[[:alpha:]]{1,}","MM",string1)
#[1] "MM, MM, Calf"
Other way would be:
?paste(gsub("^R.*\\w+","MM",strsplit(string1, ",
")[[1]]),collapse=", ")
#[1] "MM, MM, Calf"
A.K.
Hi all,
I want to replace all words that begin with a given character
with a different word. Tried gsub and str_replace_all but with little
success. In this example I want to replace all words starting with R
with MM. gsub replaces properly only once:
> gsub("^R*\\w+", "MM", "Red, Rome, Ralf")
[1] "MM, Rome, Ralf"
Thanks in advance