0
我有一個關鍵字列表:伯爵一號實例與R中沒有重複計數
library(stringr)
words <- as.character(c("decomposed", "no diagnosis","decomposition","autolysed","maggots", "poor body", "poor","not suitable", "not possible"))
我想匹配這些關鍵字的數據幀列文本(DF $文本)和計數一個關鍵字在一個不同data.frame(matchdf)發生的次數:
matchdf<- data.frame(Keywords=words)
m_match<-sapply(1:length(words), function(x) sum(str_count(tolower(df$text),words[[x]])))
matchdf$matchs<-m_match
然而,我注意到,該方法計算一列內的關鍵詞的每次出現。例如)
"The sample was too decomposed to perform an analysis. The decomposed sample indicated that this animal was dead for a long time"
然後會返回2的計數。但是,我只想計算字段中「decomposed」的第一個實例。
我認爲會有一種方法只計算使用str_count
的第一個實例,但似乎沒有一個。
你不想'str_detect'然後? – CPak