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" |
example_characters <- "xxx?ß=)(/&%$§!+~*yyy" # Our exemplifying character string example_characters # "xxx?ß=)(/&%$§!+~*yyy"
install.packages("stringr") # Install stringr package library("stringr") # Load stringr package |
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" |
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" |
str_replace_all(example_characters, "[^[:alnum:]]", "") # Removing all non-alphanumeric # "xxxßyyy"