Remove All Special Characters from String in R (2 Examples)
In this R programming tutorial you’ll learn how to delete punctuation and non-alphanumeric characters from a character string.
The article will consist of two examples for the removal of special characters. To be more precise, the content of the page looks as follows:
It’s time to dive into the examples!
Example Data & Packages
The following data is used as basement for this R programming language tutorial:
my_string <- "aaa!$%bbbêéè)(/&ßßß" # Example string my_string # Print example string # "aaa!$%bbbêéè)(/&"
As you can see based on the previous output of the RStudio console, the example data is a character string containing many special characters.
For the examples of this tutorial, we’ll also need to install and load the stringr package:
install.packages("stringr") # Install stringr package library("stringr") # Load stringr
The stringr package includes the str_replace_all function, which we will use in the examples of this tutorial.
Example 1: Remove All Non-Alphanumeric Characters
In this Example, I’ll show how to extract all non-alphanumeric characters from our character string.
str_replace_all(my_string, "[^[:alnum:]]", "") # Delete non-alphanumeric # "aaabbbêéè"
Example 2: Remove All Punctuation Characters
The following R programming code explains how to clean punctuation (also called interpunction) from our string.
str_replace_all(my_string, "[[:punct:]]", "") # Delete punctuation # "aaa$bbbêéè"
In contrast to Example 1, the $-sign is kept.
Video & Further Resources
Do you need further information on the R syntax of this tutorial? Then you might want to have a look at the following video of my YouTube channel. I’m explaining the R programming codes of the present tutorial in the video.
The YouTube video will be added soon.
Also, you could read the related tutorials of my website:
- Replace Specific Characters in String
- str_replace & str_replace_all Functions in R
- Introduction to the stringr Package
- The R Programming Language
To summarize: In this post you learned how to strip special characters in R programming. In this tutorial, I have shown how to remove characters in a single data object. However, we could apply the same syntax to a vector variable or an entire data frame column. In case you have additional comments and/or questions, let me know in the comments section below.
Statistics Globe Newsletter