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.
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.
You don't need to escape anything if you don't use a regex:
gsub('\"', "", test, fixed = TRUE)
#[1] "LAST4"
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