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…

 

Example Data

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.

 

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.

 

Subscribe to the Statistics Globe Newsletter

Get regular updates on the latest tutorials, offers & news at Statistics Globe.
I hate spam & you may opt out anytime: Privacy Policy.


6 Comments. Leave new

  • Hey Joachim, how do i replace a special character in my string? for example, i would like to replace only one paranthesis “(” which is at the end of my string with say a blank space “” or a “.”; however, i am getting an error saying “incorrectly nested paranthesis”

    Reply
  • Hi,
    Would it be possible to instead replace the whole string instead of replacing just the indicated pattern in the string using the same function?
    So in your example, replacing every value in a dataframe containing a “y” with “NEW”.

    Reply
    • Hey Jazz,

      Do you mean like this?

      x <- c("abc", "yay", "xyx", "asdf")
      x
      # [1] "abc"  "yay"  "xyx"  "asdf"
       
      x_new <- x
      x_new[grepl("y", x_new)] <- "NEW"
      x_new
      # [1] "abc"  "NEW"  "NEW"  "asdf"

      Regards,
      Joachim

      Reply
  • Sergei Abramov
    October 22, 2022 4:27 pm

    Hi Joachim. I have problem.
    string vector
    x<- c("sand silt clay", "grvl sand slt cl","cob grvl snd slt cl")
    And I wanna replace all "cl" by "clay"
    if I did use gsub("cl", "clay",x) , i get :
    "sand silt clayay" "grvl sand slt clay" "cob grvl snd slt clay"
    What function needs to be used for this case?

    Reply
    • Hey Sergei,

      I’m sorry for the delayed response. I was on a long vacation, so unfortunately I wasn’t able to get back to you earlier. Do you still need help with your question?

      Regards,
      Joachim

      Reply

Leave a Reply

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

Fill out this field
Fill out this field
Please enter a valid email address.

Top