0

I am using the R programming language. I am copying text data from a website that contains many quotation marks, i.e. "" . When I try to create a data frame that contains this text, I will get an error because of conflicting "" symbols.

For example:

a <- " "blah" blah blah"

Error: unexpected symbol in "a <- " "blah"

Normally, I would have tried to use the gsub() function to remove these quotation marks from the data frame, but I can not even create the data frame to begin with. Of course, I could bring this text into a word processing software and click " ctrl + H" to replace all quotation marks ("") with an empty space (). But is there a way to do this in R itself?

Thanks

stats_noob
  • 5,401
  • 4
  • 27
  • 83

1 Answers1

1

The typical way you would handle this would be to escape the literal double quotes with backslash:

a <- " \"blah\" blah blah"
[1] " \"blah\" blah blah"

You could also wrap your string literal inside single quotes and then not even have to escape the double quotes:

a <- ' \"blah\" blah blah'
[1] " \"blah\" blah blah"
Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360
  • thank you for your answer! the only problem is, this text contains about 10,000 words and there are many quotation marks "". It would be extremely inefficient for me to manually write these backslashes everywhere. Is there a way to just wrap everything together in one shot? – stats_noob Apr 09 '21 at 05:55
  • 1
    Well if your text _doesn't_ have single quotes, then just use my second example, and it should resolve your problem. If not, then you may have to do a regex replacement on the text before bringing it into your R script. But...why not just read such a large text using `read.csv` ? – Tim Biegeleisen Apr 09 '21 at 05:56
  • my text also has single quotes :( in your second example, you still manually placed backslashes around the double quotes? – stats_noob Apr 09 '21 at 05:58
  • 1
    [Just do a regex replacement outside of R](https://regex101.com/r/I23mOH/1). – Tim Biegeleisen Apr 09 '21 at 05:59
  • Any regex enabled text editor can help you here. I was only showing you _how_ to do the replacement on an internet demo, that is all. – Tim Biegeleisen Apr 09 '21 at 06:01