How to Use str_replace_all() in R


Often you may want to replace all matches of a particular pattern in a string in R with a different pattern.

One of the easiest ways to do so is by using the str_replace_all() function from the stringr package in R, which can be used to perform this exact task.

The str_replace_all() function uses the following syntax:

str_replace_all(string, pattern, replacement)

where:

  • string: Name of an input character vector
  • pattern: The pattern to look for
  • replacement: The replacement pattern to use

The following example shows how to use the str_replace_all() function in practice to replace all occurrences of specific patterns in strings in R.

Note: Before using the str_replace_all() function, you may need to first install the stringr package. You can use the following syntax to do so:

install.packages('stringr')

Once the stringr package has been installed, you can proceed to use the str_replace_all() function.

Example: How to Use the str_replace_all() Function in R

Suppose that we create the following vector that contains various strings:

#create vector of strings
my_strings <- c('hey hey there', 'oh hey', 'hello everyone', 'heyo how are you')

#view vector
my_strings

[1] "hey hey there"    "oh hey"           "hello everyone"   "heyo how are you"

Suppose that we would like to replace each occurrence of “hey” with “hi” in each string.

We can use the str_replace_all() function with the following syntax to do so:

library(stringr)

#replace all occurrences of "hey" with "hi" in each string
str_replace_all(my_strings, 'hey', 'hi')

[1] "hi hi there"     "oh hi"           "hello everyone"  "hio how are you"

Notice that each occurrence of “hey” has been replaced with “hi” in each string.

Also notice that only the exact substring of “hey” is replaced in each string.

For example, in the last string we can see that “heyo” has now been converted to “hio” since only the “hey” substring was replaced.

Note: The str_replace() function from the stringr package will only replace the first occurrence of a particular pattern in a string. Feel free to use that function if you don’t want to replace each occurrence of a particular pattern.

Also note that you can specify multiple patterns to replace within the str_replace_all() function.

For example, we can use the following syntax to make the following replacements:

  • Replace each occurrence of “hey” with “hi”
  • Replace each occurrence of “everyone” with “everybody”
library(stringr)

#replace all occurrences of multiple patterns in strings
str_replace_all(my_strings, c('hey'='hi', 'everyone'='everybody'))

[1] "hi hi there"     "oh hi"           "hello everybody" "hio how are you"

Notice that each occurrence of “hey” and “everyone” has been replaced with the exact replacement strings that we specified.

Feel free to use similar syntax to replace even more patterns in your own strings.

It’s also worth noting that the str_replace_all() function is case-sensitive, which means that the exact case of the pattern that we search for must match or else no replacement will be made.

Note: You can find the complete documentation for the str_replace_all() function here.

Additional Resources

The following tutorials explain how to perform other common tasks in R:

How to Use str_remove in R
How to Use str_match in R
How to Use str_pad in R

Featured Posts

Leave a Reply

Your email address will not be published. Required fields are marked *