【发布时间】:2018-02-02 20:27:41
【问题描述】:
我正在尝试将字符串 data.frame 中出现的字符串替换为另一个字符串 data.frame 中的另一个字符串。
应替换子字符串的多个基本字符串
# base strings which I want to replace
base <- data.frame(cmd = rep("this is my example <repl1> and here second <repl2> ...", nrow(repl1)))
替换字符串
# definition of replacement strings
repl1 <- data.frame(as.character(1:10))
repl2 <- data.frame(as.character(10:1))
我尝试使用 lapply 遍历 data.frame...
# what I have tried
lapply(base, function(x) {gsub("<repl1>", repl1, x)})
结果我比关注...
[1] "this is my example c(1, 3, 4, 5, 6, 7, 8, 9, 10, 2) and here second <repl2> ..."
[2] "this is my example c(1, 3, 4, 5, 6, 7, 8, 9, 10, 2) and here second <repl2> ..."
[3] "this is my example c(1, 3, 4, 5, 6, 7, 8, 9, 10, 2) and here second <repl2> ..."
但我想实现...
[1] "this is my example 1 and here second 10 ..."
[2] "this is my example 2 and here second 9 ..."
[3] "this is my example 3 and here second 8 ..."
感谢每个建议 :)
【问题讨论】:
标签: r string dataframe replace