Reputation: 21
I have a list of words included in the data frame called remove
. I want to remove all the words in text
. I want to remove the exact words.
remove <- data.frame("the", "a", "she")
text <- c("she", "he", "a", "the", "aaaa")
for (i in 1:3) {
text <- gsub(data[i, 1], "", text)
}
Attached is the result returned
#[1] "" "he" "" "" ""
However what I am expecting is
#[1] "" "he" "" "" "aaaa"
I also tried the following code, but it does return the expected result:
for (i in 1:3) {
text <- gsub("^data[i, 1]$", "", text)
}
Thanks so much for your help.
Upvotes: 1
Views: 467
Reputation: 3053
A simple base R solution is:
text[!text %in% as.vector(unlist(remove, use.names = FALSE))]
Upvotes: 1
Reputation: 32548
For exact match, use value matching (%in%
)
remove<-c("the","a","she") #I made remove a vector too
replace(text, text %in% remove, "")
#[1] "" "he" "" "" "aaaa"
Upvotes: 1