删除 R 中的反斜杠和引号
Remove Backslash and Quotations in R
如何在 R 中删除以下字符串中的引号?
test = "\"LAST4\""
noquote(test)
[1] "LAST4"
我正在手动读取数据,无法删除引号和反斜杠。
尝试:
gsub("\\"","",test)
#[1] "LAST4"
修改 :
@Roland 的解决方案提高了可读性和性能:
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
如果您不使用正则表达式,则无需转义任何内容:
gsub('\"', "", test, fixed = TRUE)
#[1] "LAST4"
"R - gsub replacing backslashes" 中的解决方案对我有用。
示例:
library(stringr)
df$variable <- str_replace(df$variable,"\\\\","")
df$variable before: "xyz\"
df$variable after:"xyz"
如何在 R 中删除以下字符串中的引号?
test = "\"LAST4\""
noquote(test)
[1] "LAST4"
我正在手动读取数据,无法删除引号和反斜杠。
尝试:
gsub("\\"","",test)
#[1] "LAST4"
修改 :
@Roland 的解决方案提高了可读性和性能:
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
如果您不使用正则表达式,则无需转义任何内容:
gsub('\"', "", test, fixed = TRUE)
#[1] "LAST4"
"R - gsub replacing backslashes" 中的解决方案对我有用。
示例:
library(stringr)
df$variable <- str_replace(df$variable,"\\\\","")
df$variable before: "xyz\"
df$variable after:"xyz"