?">

Remove all special characters from a string in R?

912    Asked by artiTrivedi in Data Science , Asked on Jun 21, 2021

 How to remove all special characters in a given string in R and replace each special character with space?

The special characters to remove are : ~!@#$%^&*(){}_+:"<>?,./;'[]-=

But how to remove for example these characters from foreign languages: â í ü Â á ą ę ś ć?

Answered by Faith Davidson

R remove character from string, but for this you have to use the string_replace_all function from the stringr package as follows:

To remove all the punctuation characters:
x <- "a1~!@#$%^&*(){}_+:"<>?,./;'[]-="
str_replace_all(x, "[[:punct:]]", " ")
[1] "a1~ $ ^ + <> ="
To remove all the non-alphanumeric characters:
 str_replace_all(x, "[^[:alnum:]]", " ")
[1] "a1
You can also use the gsub function from the base package as follows:
gsub("[^[:alnum:]]", " ", x)
[1] "a1

Your Answer

Interviews

Parent Categories