用R中的多个字符串替换相同的文本
以下是我的示例数据:用R中的多个字符串替换相同的文本,r,text,data-manipulation,R,Text,Data Manipulation,以下是我的示例数据: root <- c("how to manage xxx","how to run xxx","how to operate xxx") type <- c("resturant","grocery store","retail store") regmatchesregmatches我需要从每个查询的根目录和类型中进行轮换,如问题所示。@RanTao您现在已经编辑了问题。一些答案是使用当时可用的信息给出的。花点时间问你想问的问题。@RanTao-那不是替换操
root <- c("how to manage xxx","how to run xxx","how to operate xxx")
type <- c("resturant","grocery store","retail store")
regmatchesregmatches我需要从每个查询的根目录和类型中进行轮换,如问题所示。@RanTao您现在已经编辑了问题。一些答案是使用当时可用的信息给出的。花点时间问你想问的问题。@RanTao-那不是替换操作。你正在创造全新的价值观。无论如何,请参见“编辑我需要从根目录中的每个查询进行轮换”并键入如问题所示的内容。@RanTao您现在已经编辑了问题。一些答案是使用当时可用的信息给出的。花点时间问你想问的问题。@RanTao-那不是替换操作。你正在创造全新的价值观。无论如何,请参见编辑
kw <- gsub("xxx", "123", root)
how to manage restaurant
how to run restaurant
how to operate resturant
how to manage grocery store
...
how to operate retail store
regmatches(root, regexpr("xxx",root)) <- type
root
#[1] "how to manage resturant" "how to run grocery store"
#[3] "how to operate retail store"
out <- rep(root,each=length(type))
regmatches(out, regexpr("xxx",out)) <- type
out
#[1] "how to manage resturant" "how to manage grocery store"
#[3] "how to manage retail store" "how to run resturant"
#[5] "how to run grocery store" "how to run retail store"
#[7] "how to operate resturant" "how to operate grocery store"
#[9] "how to operate retail store"
data.frame(x = unlist(lapply(type, function(x) gsub("xxx",x,root))))
# x
#1 how to manage resturant
#2 how to run resturant
#3 how to operate resturant
#4 how to manage grocery store
#5 how to run grocery store
#6 how to operate grocery store
#7 how to manage retail store
#8 how to run retail store
#9 how to operate retail store