【问题标题】:Can't seem to get vectorized gsub to work with library "qdap" and mgsub [duplicate]似乎无法让矢量化 gsub 与库“qdap”和 mgsub 一起使用 [重复]
【发布时间】:2017-06-02 19:14:31
【问题描述】:

我在看这个问题:

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"

【问题讨论】:

    标签: r regex vectorization qdap


    【解决方案1】:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-10-04
      • 2022-12-11
      • 1970-01-01
      • 1970-01-01
      • 2019-08-01
      • 1970-01-01
      相关资源
      最近更新 更多