2017-06-27 16 views
1

私はremoveと呼ばれるデータフレームに含まれる単語のリストを持っています。 textのすべての単語を削除したいと思います。私は正確な言葉を削除したい。添付gsub関数、パターンの完全一致

remove <- data.frame("the", "a", "she") 
text <- c("she", "he", "a", "the", "aaaa") 

for (i in 1:3) { 
    text <- gsub(data[i, 1], "", text) 
} 

結果が

​​

は、しかし、私は期待してい何

#[1] "" "he" "" "" "aaaa" 

で返され、私はまた、次のコードを試してみましたが、それは期待した結果を返す行います

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" 

答えて

1

単純基地Rソリューションは:

text[!text %in% as.vector(unlist(remove, use.names = FALSE))] 
+1

動作すること。あなたの助けをありがとう! – aalen

1

について

+1

ご協力いただきありがとうございます! – aalen