Deal with Special Characters in Functions (2 Examples)


In this R tutorial you’ll learn how to deal with special characters in functions such as gsub, grepl, and gregexpr.

Table of contents:

Let’s start right away…


Creating Exemplifying Data

I use the following data as basement for this R tutorial:

x <- "xxx $ xx ? xxxx [ xx \\ xxx"    # Create example string
x                                     # Print character string
# [1] "xxx $ xx ? xxxx [ xx \\ xxx"

The previous output of the RStudio console shows that our example data object contains a random sequence of characters. Some of these characters are special characters such as $?[\.


Example 1: Replacing Special Characters Using \\

The following R code explains how to manipulate special characters within a function.

For this example, we’ll use the gsub function. Please note that we could apply this logic to other types of functions that are taking character strings as input.

However, let’s try to replace the $ sign in our character string using the gsub function:

gsub("$", "NEW", x)                   # Trying to replace special character
# [1] "xxx $ xx ? xxxx [ xx \\ xxxNEW"

As you can see based on the previous output of the RStudio console, we have not replaced the $ sign. Instead, we have added our new character pattern at the end of the character string. That’s not what we wanted!

To be able to use special characters within a function such as gsub, we have to add two backslashes (i.e. \\) in front of the special character.

The following R code replaces the $ sign…

gsub("\\$", "NEW", x)                 # Replacing $
# [1] "xxx NEW xx ? xxxx [ xx \\ xxx"

…the next R syntax replaces the question mark…

gsub("\\?", "NEW", x)                 # Replacing ?
# [1] "xxx $ xx NEW xxxx [ xx \\ xxx"

…and the following code replaces the square bracket:

gsub("\\[", "NEW", x)                 # Replacing [
# [1] "xxx $ xx ? xxxx NEW xx \\ xxx"

Looks good!

We can use the previous type of R code for basically any special character. However, there is one exception… So keep on reading!


Example 2: Replacing Backslash Using \\\

This example illustrates how to handle character backslashes within functions. First, let’s have a look at the following R code:

gsub("\\\", "NEW", x)                 # Trying to replace \
# Error: unexpected symbol in "gsub("\\\", "NEW"

We have tried to use a double backslash before the character backslash that we wanted to replace. However, the RStudio console returned an error message.

If we want to replace a character backslash, we have to add three backslashes in front of the character backslash (i.e. four backslashes in total). Let’s do this:

gsub("\\\\", "NEW", x)                # Replacing \
# [1] "xxx $ xx ? xxxx [ xx NEW xxx"

Have a look at the previous output: We have replaced the character backslash by our new character pattern.


Video & Further Resources

Would you like to know more about character strings in R? Then you may watch the following video that I have published on my YouTube channel. In the video, I show the topics of this tutorial.



Furthermore, you might have a look at the related articles on my homepage.


Summary: You have learned in this article how to handle special characters in R programming. If you have any additional questions or comments, let me know in the comments section. Furthermore, please subscribe to my email newsletter in order to get updates on new tutorials.


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.

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.