dapangmao
dapangmao

Reputation: 2797

How to remove backslash in a string by R

I need to remove all the backslash in a string like below. x <- ' [{\"Petal.Width\" : 1.8, \"Species\" : \"virginica\" } ] '

The resulting string I want is [{"Petal.Width" : 1.8, "Species" : "virginica" } ]

I tried some methods which don't work. gsub("\\\\", "\\", x)

Could any people suggest some methods? Many thanks!

Upvotes: 2

Views: 4283

Answers (1)

BenBarnes
BenBarnes

Reputation: 19454

The backslashes are R's way of escaping quotation marks within a character string (see ?Quotes). It is unlikely that you'll have to remove them. For example, if you're looking to use your object x as a title for a plot, the backslashes will not appear:

plot(1, 1, main = x)

enter image description here

Upvotes: 3

Related Questions