gsub and backslashes in R

syntheso

I have the following string:

c(\"Yes \", \" No\")

I want to remove the backslashes such that it becomes:

c("Yes", "No)

I tried the following, as suggested here:

gsub("\\","", "c(\"Yes \", \" No\")", fixed = TRUE)

But it does not work, so I must have misunderstood something.

akrun

It is a double quote, so either remove the quote and replace with blank

gsub('"',"", "c(\"Yes \", \" No\")", fixed = TRUE)
#[1] "c(Yes ,  No)"

or replace with a single quote

gsub('"',"'", "c(\"Yes \", \" No\")", fixed = TRUE)
#[1] "c('Yes ', ' No')"

If we print with cat, the character \ doesn't exist

cat("c(\"Yes \", \" No\")")
#c("Yes ", " No")


nchar('\"')
#[1] 1

cat('\"')
#"

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related