我在看这个问题:
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"
答案 0 :(得分:1)
您需要指定fixed = FALSE
,默认为TRUE
,它会按原样替换模式,不使用正则表达式:
result <- mgsub(pat, repl, testList, fixed = FALSE)
result
# [1] "apple" "orange" "banana" "apple" "orange" "banana"