2
我有以下的數據幀:精確匹配於單詞列表字對於每一列
word sentence
cat the cat was red
blue the cat was red
dog the dogs
我想添加取決於字中是否有精確匹配的0的新列或1句子,即
word sentence isInSentence
cat the cat was red 1
blue the cat was red 0
dog the dogs 0
我發現匹配函數可以爲一個字符串向量中的單詞做到這一點。但是,直接申請比賽時
ifelse(match(d$word, strsplit(d$sentence, ' '), nomatch=0) == 0, 0, 1)
它不按預期工作。我認爲它不是按行執行匹配操作,因爲我願意。我也研究過grep,但是我一直無法找到一種方法來讓這兩個函數做我想做的事情。
有什麼建議嗎?
謝謝!