R:通过一个字符串矢量为字符串

问题描述:

内更换所有实例如果我有:R:通过一个字符串矢量为字符串

mystring<-"I have one cat, two dogs and three rabbits" 
numlist<-c("one","two","three") 

我如何通过numlist成类似gsub和更换匹配的所有实例mystring让我得到:

"I have ##NUMBER## cat, ##NUMBER## dogs and ##NUMBER## rabbits" 

我曾尝试:

> lapply(mystring,arg1=numlist,function(x,arg1) gsub(arg1,"##NUMBER##",x)) 
[[1]] 
[1] "I have ##NUMBER## cat, two dogs and three rabbits" 

Warning message: 
In gsub(arg1, "##NUMBER##", x) : 
    argument 'pattern' has length > 1 and only the first element will be used 

因为... e gsub不是矢量化的。但是我认为lapply可以照顾这个?

我们可以使用gsubfn如果我们需要用数字来代替。

library(gsubfn) 
gsubfn("\\w+", as.list(setNames(1:3, numlist)), mystring) 
#[1] "I have 1 cat, 2 dogs and 3 rabbits" 

编辑:我认为我们需要用对应于'numlist'中的单词的数字替换。但是,当且仅当我们需要##NUMBER##标志替换,一种选择是mgsub

library(qdap) 
mgsub(numlist, "##NUMBER##", mystring) 
#[1] "I have ##NUMBER## cat, ##NUMBER## dogs and ##NUMBER## rabbits" 
+1

我相信如果你运行基准测试,mgsub是迄今为止最快的解决方案。 –

+1

谢谢 - 这对我有用。 – brucezepplin

您可以使用lapply您可以从您的搜索字符串构造正则表达式:

gsub(paste(numlist, collapse = '|'), '##NUMBER##', mystring) 

这将匹配在numlist任何字符串。

当使用lapply时,您需要颠倒您的参数,因为您要将该功能应用于numlist而不是mystring;此外,您的功能必须采用一个参数:

lapply(numlist, function (num) gsub(num, '##NUMBER##', mystring)) 

但是,这会产生不同的结果;即,它将返回结果串,每一个不同的字代替:

[[1]] 
[1] "I have ##NUMBER## cat, two dogs and three rabbits" 

[[2]] 
[1] "I have one cat, ##NUMBER## dogs and three rabbits" 

[[3]] 
[1] "I have one cat, two dogs and ##NUMBER## rabbits" 
+0

而更换好,我真的必须只有一个结果返回。 – brucezepplin

不是一个优雅的方式,但它的作品,

x <- "I have ##NUMBER## cat, ##NUMBER## dogs and ##NUMBER## rabbits" 
numlist <- c("one","two","three") 

for (i in 1:length(numlist)) { 
    loc <- regexpr("##NUMBER##", x) 
    start_loc <- loc[[1]] 
    width <- attr(loc, "match.length") 
    x <- paste(substr(x, 1, start_loc - 1), numlist[i], substr(x, start_loc + width, nchar(x)), sep = "") 
} 

输出:

> x 
[1] "I have one cat, two dogs and three rabbits"