如果我有数据框,则值为:
df<- c("One", "Two Three", "Four", "Five")
df<-data.frame(df)
df
"One"
"Two Three"
"Four"
"Five"
我还有另一个数据框,例如:
df2<-c("the park was number one", "I think the park was number two three", "Nah one and two is ok", "tell me about four and five")
df2<-data.frame(df2)
df2
the park was number one
I think the park was number two three
Nah one and two is ok
tell me about four and five
如果在df2 [,1]的任何字符串中找到df中找到的值之一,我该如何用&#34; it&#34;这样的单词替换它。
我想用这个替换我最后的df2:
df3
the park was number it
I think the park was number it
Nah it and two is ok
tell me about it and it
我知道这可能与以下内容有关:
gsub(df,"it", df2)
但我不认为这是对的。
谢谢!
答案 0 :(得分:1)
您可以执行类似
的操作sapply(df$df,function(w) df2$df2 <<- gsub(paste0(w,"|",tolower(w)),"it",df2$df2))
df2
df2
1 the park was number it
2 I think the park was number it
3 Nah it and two is ok
4 tell me about it and it
<<-
运算符确保函数更改了全局环境中df2
的版本。 paste0(w,"|",tolower(w))
允许大写字母的差异,如您的示例所示。
请注意,您应该在问题中的数据框定义中添加stringAsFactors=FALSE
。