Replace Specific Characters in String in R (4 Examples)
This article illustrates how to exchange a certain character pattern within a string in the R programming language.
Table of contents:
Let’s dive right in…
Let’s first define some example data:
x <- "xxxxyxxyxaaaaaay" # Create example character string x # Print character string # "xxxxyxxyxaaaaaay"
As you can see based on the previous output of the RStudio console, our example data is a character string containing of the letters x, y, and a.
Example 1: Replace All Occurrences of Specific Character in String
In this Example, I’ll show how to replace all appearances of the letter y by the character pattern NEW. For this, we can use the gsub function as shown below:
gsub("y", "NEW", x) # Applying gsub # "xxxxNEWxxNEWxaaaaaaNEW"
As you can see based on the previous output, our updated character string contains the character pattern NEW instead of the alphabetical letter y.
Example 2: Replace First Occurrence of Specific Character in String
Example 2 shows how to use the sub function to exchange only the first occurrence of the letter y in our character string. Have a look at the following R code:
sub("y", "NEW", x) # Applying sub # "xxxxNEWxxyxaaaaaay"
As you have seen in this and the previous example, the gsub function is used to replace all occurrences of a character patter and the sub function is used to replace only the first occurrence.
Example 3: Replace All Occurrences Using str_replace_all Function of stringr Package
The stringr package is a powerful add-on package for the manipulation of character strings in R.
For that reason, I want to show in Examples 3 and 4, how to use the functions of the stringr package to replace certain characters in strings.
If we want to use the commands & functions of the stringr package, we first need to install and load stringr:
install.packages("stringr") # Install stringr package library("stringr") # Load stringr package
Now, we can apply the str_replace_all function of the stringr package to replace all occurrences of the letter y.
str_replace_all(x, "y", "NEW") # Applying str_replace_all # "xxxxNEWxxNEWxaaaaaaNEW"
The previous output is exactly the same as in Example 1.
Example 4: Replace First Occurrence Using str_replace Function of stringr Package
In this Section, I’ll show how to exchange only the first appearance of a character in a string using the str_replace of the stringr package.
str_replace(x, "y", "NEW") # Applying str_replace # "xxxxNEWxxyxaaaaaay"
This time, the output is exactly the same as in Example 2.
Video, Further Resources & Summary
If you need more info on the examples of this page, you may watch the following video of my YouTube channel. I show the content of this article in the video:
In addition to the video, you may have a look at some of the other articles on my homepage. I have published several other tutorials about related topics such as extracting data and character strings.
- sub & gsub Functions
- str_replace & str_replace_all Functions
- Extract First or Last n Characters from String
- Remove All Special Characters from String
- R Programming Language
Summary: This article illustrated how to replace characters in strings in R programming. Let me know in the comments section below, if you have additional questions and/or comments. Furthermore, don’t forget to subscribe to my email newsletter in order to get updates on new articles.