我想更换:
(1) ", "
(逗号+空格)和 "_"
(下划线)
(2) "'"
(撇号)与"'s"
(撇号+s)
library(gsubfn)
x <- c("Mary' car is red.", "A, B, C")
gsubfn(".", list(", " = "_", "'" = "'s"), x)
我想要的是
"Mary's car is red."
和 "A_B_C"
,但结果是 "Mary's car is red."
和 "A, B, C"
。为什么? 最佳答案
试试这个:
toreplace<-list(", " = "_", "'" = "'s")
gsubfn(paste(names(toreplace),collapse="|"),toreplace,x)
#[1] "Mary's car is red." "A_B_C"
您的方法的问题在于您的模式只是一个字符(
.
)并且无法匹配 ", "
。关于r - 多字符串替换,在 R 中使用 gsubfn(),我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/34156720/