sub(pattern, replacement, x)
gsub(pattern, replacement, x)
Replace the first occurrence of a pattern with sub or replace all occurrences with gsub.
- pattern – A pattern to search for, which is assumed to be a regular expression. Use an additional argument fixed=TRUE to look for a pattern without using regular expressions.
- replacement – A character string to replace the occurrence (or occurrences for gsub) of pattern.
- x – A character vector to search for pattern. Each element will be searched separately.
Example. The simple example below does not make use of regular expressions.
> > x <- c("This is a sentence about axis", + "A second pattern is also listed here") > sub("is", "XY", x)  "ThXY is a sentence about axis"  "A second pattern XY also listed here" > gsub("is", "XY", x)  "ThXY XY a sentence about axXY"  "A second pattern XY also lXYted here" >