Replacing entire string when a single word is in the string R -
i wondering if had come across solution problem when cleaning data in r. idea have list of strings example:
strings = c("hello world", "goodbye all", "help appreciated", "hello sam")
then go through list of strings , whenever word found entire string replaced.
therefore if looking word "hello" replaced "math"
so output looking be:
"math", "goodbye all", "help appreciated", "math".
any or ideas appreciated.
just use grepl
:
strings = c("hello world", "goodbye all", "help appreciated", "hello sam") > strings[grepl("hello",strings)] <- "math" > strings [1] "math" "goodbye all" "help appreciated" "math"
Comments
Post a Comment