str_replace() replaces the first match in each string element
str_replace_all() replaces all matches in each string element
`str_replace_na() replaces missing value NA into character "NA"
str_replace() is similar to str_extract(), but instead of extracting the matched component, it replaces it with a new string.
In each string element, only the first match is replaced.
The pattern argument takes a specific string, or a regular expression as input. Special characters need to be escaped by the double-backslash to be interpreted literally, instead of as a functional operator.
For the replacement argument, it is not mandatory to escape special characters by the double-backslash; what is inside the quote will be treated literally.
library(stringr) phones <-c("Mia Smith, 728*971*9652", "Max Lee, 683*976*9876","Ava Johnson, 912*254*3387") # replace the first asterisk with a dot.str_replace(phones, pattern ="\\*", # search for a single asteriskreplacement =".")
The above code can be slightly simplified using rebus package. While WRD matches only a single letter or digit, str_replace_all() repeats such replacement across the entire string.