Reputation: 779
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'."
Upvotes: 6
Views: 4218
Reputation: 1
I used a combination of \ and single quotes around double quotes in a similar situation I had.
I wanted to pass the following string into a function as a test
"count(grepl("[()']", df$var)==T)>0"
but the [()'] became unquoted, the solution was
"count(grepl("'[()']'", df$var)==T)>0"
Upvotes: 0
Reputation: 726
y <- "I\'m watching 'Prometheus'."
[1] "I'm watching 'Prometheus'."
y <- "I\'m watching Prometheus."
[1] "I'm watching Prometheus."
Upvotes: 1
Reputation: 4939
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)
Upvotes: 5
Reputation: 7475
Im probably not getting it but
gsub("\"","","I'm watching \"Prometheus\".")
or
gsub("\"","'","I'm watching \"Prometheus\".")
?
Upvotes: 4