Solve Double qoutes in double quotes in R

With R, when we put double quotes inside double quotes:

y <- " " " "

It will end

Error: Unexpected string constant in "y <-" "" "

My question is how to remove all double quotes found or converted to single quotes in main double quotes.

For instance:

y <- "I'm watching "Prometheus"." 
y

Desired Result:

#[1] "I'm watching Prometheus."

or

#[1] "I'm watching 'Prometheus'."
+5
source share
3 answers

Are you parsing the string input from a file or the standard input of something?

scan(what='character',sep='\n')will read data from stdin()and automatically escape quotes. Same as from file

>scan(what="character",sep="\n",allowEscapes=T)
1: I'm watching "Prometheus"
2: 
Read 1 item
[1] "I'm watching \"Prometheus\""
>
>scan(what="character",sep="\n",allowEscapes=T)
1: "I'm watching "Prometheus""
2: 
Read 1 item
[1] "\"I'm watching \"Prometheus\"\""

Once you get your input, you can use regex to replace hidden inner quotes ... (I think! - it can be tricky reg exp)

+5

, ,

gsub("\"","","I'm watching \"Prometheus\".") 

gsub("\"","'","I'm watching \"Prometheus\".") 

?

+4
y <-  "I\'m watching 'Prometheus'."

[1] "I'm watching 'Prometheus'."

y <-  "I\'m watching Prometheus."

[1] "I'm watching Prometheus."
+1
source

All Articles