str_replace {stringr} | R Documentation |
Vectorised over string
, pattern
and replacement
.
str_replace(string, pattern, replacement) str_replace_all(string, pattern, replacement)
string |
Input vector. Either a character vector, or something coercible to one. |
pattern, replacement |
Supply separate pattern and replacement strings
to vectorise over the patterns. References of the form For |
A character vector.
str_replace_na
to turn missing values into "NA";
stri_replace
for the underlying implementation.
fruits <- c("one apple", "two pears", "three bananas") str_replace(fruits, "[aeiou]", "-") str_replace_all(fruits, "[aeiou]", "-") str_replace(fruits, "([aeiou])", "") str_replace(fruits, "([aeiou])", "\\1\\1") str_replace(fruits, "[aeiou]", c("1", "2", "3")) str_replace(fruits, c("a", "e", "i"), "-") fruits <- c("one apple", "two pears", "three bananas") str_replace(fruits, "[aeiou]", "-") str_replace_all(fruits, "[aeiou]", "-") str_replace_all(fruits, "([aeiou])", "") str_replace_all(fruits, "([aeiou])", "\\1\\1") str_replace_all(fruits, "[aeiou]", c("1", "2", "3")) str_replace_all(fruits, c("a", "e", "i"), "-") # If you want to apply multiple patterns and replacements to the same # string, pass a named version to pattern. str_replace_all(str_c(fruits, collapse = "---"), c("one" = 1, "two" = 2, "three" = 3))