gsub 函数,模式的精确匹配
gsub function, exact match of the pattern
我有一个包含在名为 remove
的数据框中的单词列表。我想删除 text
中的所有单词。我想删除确切的词。
remove <- data.frame("the", "a", "she")
text <- c("she", "he", "a", "the", "aaaa")
for (i in 1:3) {
text <- gsub(data[i, 1], "", text)
}
附上结果 returned
#[1] "" "he" "" "" ""
然而我期待的是
#[1] "" "he" "" "" "aaaa"
我也试过下面的代码,但它 return 预期的结果:
for (i in 1:3) {
text <- gsub("^data[i, 1]$", "", text)
}
非常感谢您的帮助。
对于精确匹配,使用值匹配 (%in%
)
remove<-c("the","a","she") #I made remove a vector too
replace(text, text %in% remove, "")
#[1] "" "he" "" "" "aaaa"
一个简单的基础 R 解决方案是:
text[!text %in% as.vector(unlist(remove, use.names = FALSE))]
我有一个包含在名为 remove
的数据框中的单词列表。我想删除 text
中的所有单词。我想删除确切的词。
remove <- data.frame("the", "a", "she")
text <- c("she", "he", "a", "the", "aaaa")
for (i in 1:3) {
text <- gsub(data[i, 1], "", text)
}
附上结果 returned
#[1] "" "he" "" "" ""
然而我期待的是
#[1] "" "he" "" "" "aaaa"
我也试过下面的代码,但它 return 预期的结果:
for (i in 1:3) {
text <- gsub("^data[i, 1]$", "", text)
}
非常感谢您的帮助。
对于精确匹配,使用值匹配 (%in%
)
remove<-c("the","a","she") #I made remove a vector too
replace(text, text %in% remove, "")
#[1] "" "he" "" "" "aaaa"
一个简单的基础 R 解决方案是:
text[!text %in% as.vector(unlist(remove, use.names = FALSE))]