gpt4 book ai didi

r - 使用 dplyr (R) 计算字符串中的唯一单词

转载 作者:行者123 更新时间:2023-12-05 01:23:19 25 4
gpt4 key购买 nike

假设我有一个字符串如下:

string <- "the home home on the range the friend"

我想做的就是确定字符串中哪些单词至少出现了 2 次。

这里的伪代码是:

  • 计算每个单词出现的次数
  • 返回在字符串中出现两次以上的单词列表

最终结果应该是一个包含 thehome 的列表,并按此顺序排列。

我希望使用 tidyverse 来做到这一点,最好是使用 stringr 或 dplyr。也曾尝试使用 tidytext,但一直在努力。

最佳答案

我们可以按空格拆分字符串,得到和基于频率的子集

out <- table(strsplit(string, "\\s+")[[1]])
out[out >=2]

home the
2 3

关于r - 使用 dplyr (R) 计算字符串中的唯一单词,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/72595909/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com