R Remove Punctuation & Non-Alphanumeric Characters from String

In this article you’ll learn how to delete special characters in the R programming language.

Setting up the Examples

example_characters <- "xxx?ß=)(/&%$§!+~*yyy"             # Our exemplifying character string
example_characters
# "xxx?ß=)(/&%$§!+~*yyy"
install.packages("stringr")                                # Install stringr package
library("stringr")                                         # Load stringr package

Example 1: Deleting All Punctuation Characters in String

str_replace_all(example_characters, "[[:punct:]]", "")   # Removing all punctuation
# "xxxß=$+~yyy"

Example 2: Deleting All Non-Alphanumeric Characters in String

str_replace_all(example_characters, "[^[:alnum:]]", "")  # Removing all non-alphanumeric
# "xxxßyyy"

Leave a Reply

Your email address will not be published.

Fill out this field
Fill out this field
Please enter a valid email address.
You need to agree with the terms to proceed

Menu
Top