我有一組語句搜索從關鍵詞和發生的標籤關鍵字的列表
statement <- as.matrix(c("the cat sat on the mat",
"the dog ran up the hill",
"the dog ran up the hill to the mat"))
和關鍵字
keywords <- as.matrix(c("cat", "mat", "dog", "hill"))
我想中的搜索列表來自我的關鍵字列表並標記出現了什麼關鍵字,即有結果
statement keywords
the cat sat on the mat cat, mat
the dog ran up the hill dog, hill
the dog ran up the hill to the mat dog, hill, mat
我想一個辦法,我能做到這一點是使用grep在某種程度上像
statement[grep("cat", statement$V1, ignore.case = TRUE), "keywords"] <- "cat"
statement[grep("mat", statement$V1, ignore.case = TRUE), "keywords"] <- "mat"
...等,但首先,這將不會給標籤對我來說發生的所有關鍵字。其次,如果我想找到這樣的方式,當我有一個大名單可以說,1000個關鍵詞和語句500將只是笨拙。
你怎麼會建議一個樣?有沒有使用grep的方法,或者是否有任何可以從預定列表中挖掘文本並返回關鍵字的包?
謝謝!
是否有必要對這些是矩陣對象?或者矢量是否足夠? – Benjamin
@benjamin載體會在這種情況下是足夠 –
@DarshanBaral的感謝!這是真正有用的 –