gsub()
can find and replace pattern in a vector or a data frame.
Working example:
x <- c("abcdefghabc", "abcde")
gsub("abc", "ABC", x)
## [1] "ABCdefghABC" "ABCde"
Note that the abc
pattern appeared twice in the first element and both have been replaced with ABC
. To do a little comparison here with a very similar function sub()
:
sub("abc", "ABC", x)
## [1] "ABCdefghabc" "ABCde"
We can see that although sub()
can apply on a vector, only the first occurrence of the pattern was replaced.