计算单词在文本中出现的总次数

时间:2020-06-08 13:53:14

标签: r stringr

我有一个称为words的单词向量。我想在data.frame中创建一个新列,以计算出这些单词在该文本中出现的总次数。我正在尝试使用str_count(),但似乎无法正常工作:

data %>% 
  str_count(string = text, pattern = words)

数据:

words <- c("apple", "banana", "lemon")


data <- data.frame(
  id = c(1, 2, 3, 4, 5),
  text = c(
    "The apple is green and the banana is yellow",
    "There is a banana and another banana here",
    "The lemon is yellow and the banana is yellow, but the apple is green",
    "The apple is the same shape as the lemon",
    "lemon, lemon, apple, lemon, banana"
  )
)


expectedOutput <- data.frame(
  out = c(
    2,
    2,
    3,
    2,
    5
  )
)

0 个答案:

没有答案
相关问题