提问人:cliu 提问时间:3/3/2023 最后编辑:cliu 更新时间:3/3/2023 访问量:28
使用 group by [duplicate] 计算单词总数
Count total of words using group by [duplicate]
问:
我想按以下方式计算文本组中的单词总数: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 中比较字符向量中的值
下一个:如何将字符串转换为可用格式
评论
df %>% mutate(words=stringi::stri_count_words(tx)) %>% group_by(id) %>% summarise(sum(words))
df %>% mutate(count = stringr::str_count(tx, boundary("word"))) %>% group_by(id) %>% summarise(count = sum(count))