With R, when we put double quotes inside a double quotes:
y <- " " " "
It will end out
Error: unexpected string constant in "y <- " " " ""
My question is how to remove all the double quotes detected or convert into single quotes within the main double quotes.
For example:
y <- "I'm watching "Prometheus"."
y
The desired result is
#[1] "I'm watching Prometheus."
or
#[1] "I'm watching 'Prometheus'."
' appearing in double quotes is escaped using a backslash. The backslash preceding the ' !
In American English, use double quotation marks for quotations and single quotation marks for quotations within quotations. In British English, use single quotation marks for quotations and double quotation marks for quotations within quotations.
Single quotes need to be escaped by backslash in single-quoted strings, and double quotes in double-quoted strings. Alternative forms for the last two are '\u{nnnn}' and '\U{nnnnnnnn}'. All except the Unicode escape sequences are also supported when reading character strings by scan and read.
Using " is the way to do it.
Are you parsing string input from a file or standard input of something?
scan(what='character',sep='\n')
will read data from the stdin()
and automatically escape the quotes. Same if from a 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've got your input you could use a regular expression to replace the escaped inner quotes... (I think! - might be a complicated reg exp)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With