How to remove all special characters from string in R and replace them with spaces ?
Some special characters to remove are : ~!@#$%^&*(){}_+:"<>?,./;'[]-=
I've tried regex
with [:punct:]
pattern but it removes only punctuation marks.
Question 2 : And how to remove characters from foreign languages like : â í ü Â á ą ę ś ć
?
Answer : Use [^[:alnum:]]
to remove~!@#$%^&*(){}_+:"<>?,./;'[]-=
and use [^a-zA-Z0-9]
to remove also â í ü Â á ą ę ś ć
in regex
or regexpr
functions.
Solution in base R :
x <- "a1~!@#$%^&*(){}_+:\"<>?,./;'[]-="
gsub("[[:punct:]]", "", x) # no libraries needed
sub
orgsub
functions. – Paul Hiemstra