似乎无法使用矢量化的gsub来处理库" qdap"和mgsub

时间:2017-06-02 19:14:31

标签: r regex vectorization qdap

我在看这个问题:

R: gsub, pattern = vector and replacement = vector

我正在尝试一个简单的例子,我无法让它发挥作用。

testList <- c("apple", "orange", "banana1", "apple4", "orange 8", "banana 10")

repl <- c("apple", "orange", "banana")

pat <- paste0("^", repl, "[[:space:]]*[[:digit:]]*$")

result <- mgsub(pat, repl, testList)

不应该输出:

"apple", "orange", "banana", "apple", "orange", "banana"

1 个答案:

答案 0 :(得分:1)

您需要指定fixed = FALSE,默认为TRUE,它会按原样替换模式,不使用正则表达式:

result <- mgsub(pat, repl, testList, fixed = FALSE)
result
# [1] "apple"  "orange" "banana" "apple"  "orange" "banana"