使用 group by [duplicate] 计算单词总数

Count total of words using group by [duplicate]

提问人:cliu 提问时间:3/3/2023 最后编辑:cliu 更新时间:3/3/2023 访问量:28

问:

我想按以下方式计算文本组中的单词总数:id

df <- data.frame(id=rep(1:3, 2), tx=c("test one. test two", "this is a test. again test", "test two", "test three times", 
                                      "test, in a future time point", "test has completed, at the final time point"))

我如何实现这样的结果:

id  word count
1   7
2   12
3   10

我看了另一篇帖子 计算字符串中所有单词的数量,但它没有显示如何按组计算单词。

r 字符串 文本 数据操作 计数

评论

0赞 cliu 3/3/2023
请重新提出问题。我看了另一个问题,但它没有显示如何通过分组来计算单词
1赞 MrFlick 3/3/2023
只需数数单词,然后像使用任何其他 dplyr pipleine 一样group_by/总结:.计算字数和总结总和是两个独立的问题。df %>% mutate(words=stringi::stri_count_words(tx)) %>% group_by(id) %>% summarise(sum(words))
1赞 Jamie 3/3/2023
您可以执行以下操作df %>% mutate(count = stringr::str_count(tx, boundary("word"))) %>% group_by(id) %>% summarise(count = sum(count))
1赞 Gregor Thomas 3/3/2023
我添加了“如何按组对变量求和”的常见问题解答,作为汇总数据的参考。

答: 暂无答案