多个字符串替换,在R中使用gsubfn()

时间:2015-12-08 13:07:46

标签: r string gsub

我想替换:

(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"。为什么?

2 个答案:

答案 0 :(得分:6)

试试这个:

toreplace<-list(", " = "_", "'" = "'s")
gsubfn(paste(names(toreplace),collapse="|"),toreplace,x)
#[1] "Mary's car is red." "A_B_C"

您的方法存在的问题是您的模式只是一个字符(.)且无法与", "匹配。

答案 1 :(得分:2)

x <- c("Mary' car is red.", "A, B, C")

l <- list(r1 = c("'","'s"),
          r2 = c(', ','_'))

gsub2 <- function(l, x, ...)
  do.call('gsub', c(list(x = x, pattern = l[1], replacement = l[2]), ...))
Reduce('gsub2', l, x, right = TRUE)
# [1] "Mary's car is red." "A_B_C"             

或更多

l <- list(r1 = c("'","'s"),
          r2 = c(', ','_'),
          r3 = c('M', 'Mmmm'),
          r4 = c('\\br', 'rrrr'),
          r5 = c('\\.', '!!!'))

Reduce('gsub2', l, x, right = TRUE)
# [1] "Mmmmary's car is rrrred!!!" "A_B_C"