-2

How do I remove the quotes from the following string in R?

test = "\"LAST4\""
noquote(test)
[1] "LAST4"

I'm reading in the data manually and I can't remove the quotations and backslashes.

user13317
  • 451
  • 3
  • 13
  • What do you mean "I'm reading the data in manually"? It would be better to fix the problem at the time of import rather than later. – MrFlick Dec 17 '15 at 15:28

3 Answers3

11

You don't need to escape anything if you don't use a regex:

gsub('\"', "", test, fixed = TRUE)
#[1] "LAST4"
Roland
  • 127,288
  • 10
  • 191
  • 288
0

Try :

gsub("\\\"","",test)
#[1] "LAST4"

AMEND :

@Roland 's solution improves both readability and performance :

require(rbenchmark)
test = "\"LAST4\""

a <- function() gsub("\\\"","",test)
b <- function() gsub('\"', "", test, fixed = TRUE)

benchmark(a(), b(), replications=10^7)
#  test replications elapsed relative user.self sys.self user.child sys.child
#1  a()     10000000  87.216    1.801    87.914        0          0         0
#2  b()     10000000  48.430    1.000    46.989        0          0         0
Vongo
  • 1,325
  • 1
  • 17
  • 27
0

The solution in: "R - gsub replacing backslashes" worked for me.

Example:

library(stringr)
df$variable <- str_replace(df$variable,"\\\\\\\","")

df$variable before: "xyz\"
df$variable after:"xyz"
m0nhawk
  • 22,980
  • 9
  • 45
  • 73
Ted
  • 91
  • 1
  • 4