Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Solve the Double qoutes within double quotes issue in R

Tags:

r

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'."
like image 739
Kai Feng Chew Avatar asked Jun 20 '12 10:06

Kai Feng Chew


People also ask

How can I escape a double quote inside double quotes?

' appearing in double quotes is escaped using a backslash. The backslash preceding the ' !

Can we use double quotes inside double quotes?

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.

How do you escape quotation marks in R?

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.

How do you escape a double quote in a title attribute?

Using &quot; is the way to do it.


1 Answers

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)

like image 119
Davy Kavanagh Avatar answered Sep 28 '22 02:09

Davy Kavanagh