Counting whole word/number occurrences with str_count in R

后端 未结 2 1673
醉酒成梦
醉酒成梦 2021-01-21 23:57

Similar to this case, i would like to count the number of occurrences of multiple words and numbers that occur in a vector of sentences with str_count of the stringr package.

2条回答
  •  不要未来只要你来
    2021-01-22 00:36

    Try putting word boundaries around your keywords:

    keywords <- c("honda","civic","toyota","auris","nissan","skyline","1988","1400","159")
    keywords <- paste0("\\b", keywords, "\\b")
    

    In regex lingo, \bhonda\b says to match the isolated word honda. Hence hondas would not match because it has an extra letter at the end.

提交回复
热议问题