alice
alice

Reputation: 33

Remove double quotes from text in r

I want to eliminate double quotes from text in R. Is there a better way to do it? I tried below code but it's still not removing double quotes:

 gsub("\"", "", a$answer)

Upvotes: 1

Views: 296

Answers (2)

akrun
akrun

Reputation: 887851

We can also do this without escaping the double quotes

gsub('"', "gone!", withquotes)
#[1] " this is a double quote: gone! "

data

withquotes <- ' this is a double quote: " '

Upvotes: 0

user2554330
user2554330

Reputation: 44977

The problem with what you tried is that you want the regular expression (i.e. pattern) to be \", but backslashes are special to R, so you need to write it twice in R so it ends up as a single backslash in the pattern.

For example,

withquotes <- ' this is a double quote: " '
gsub('\\"', "gone!", withquotes)
# [1] " this is a double quote: gone! "

Upvotes: 2

Related Questions