Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to remove specific special characters in R

I have some sentences like this one.

c = "In Acid-base reaction (page[4]), why does it create water and not H+?" 

I want to remove all special characters except for '?&+-/

I know that if I want to remove all special characters, I can simply use

gsub("[[:punct:]]", "", c)
"In Acidbase reaction page4 why does it create water and not H"

However, some special characters such as + - ? are also removed, which I intend to keep.

I tried to create a string of special characters that I can use in some code like this

gsub("[special_string]", "", c)

The best I can do is to come up with this

cat("!\"#$%()*,.:;<=>@[\\]^_`{|}~.")

However, the following code just won't work

gsub("[cat("!\"#$%()*,.:;<=>@[\\]^_`{|}~.")]", "", c)

What should I do to remove special characters, except for a few that I want to keep?

Thanks

like image 815
wen Avatar asked Feb 08 '14 03:02

wen


1 Answers

gsub("[^[:alnum:][:blank:]+?&/\\-]", "", c)
# [1] "In Acid-base reaction page4 why does it create water and not H+?"
like image 195
BrodieG Avatar answered Sep 17 '22 21:09

BrodieG