我有一个称为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
)
)