內更換所有實例如果我有: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可以照顧這個?
我相信如果你運行基準測試,mgsub是迄今爲止最快的解決方案。 –
謝謝 - 這對我有用。 – brucezepplin