在字符串中查找最常见的单词 [英] Find most common word in a character string
本文介绍了在字符串中查找最常见的单词的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个字符串,需要找到字符串中出现频率最高的单词.我已经尝试了 max
、which.max
、sort
、order
和 rank 的所有变体
我能想到的 - 但似乎无法正确计算出语法.我也尝试过这里找到的所有方法:计算频率出现在使用 R 的数组中
I have a character string and need to find the word in the string that occurs most frequently. I've tried every variation of max
, which.max
, sort
, order
, and rank
that I can think of - but can't seem to get the syntax worked out correctly. I've also tried all of the methods found here: Calculate frequency of occurrence in an array using R
示例代码:
zzz <- c("jan", "feb", "jan", "mar", "mar", "jan", "feb") #random example data
zzz <- paste(zzz, collapse=" ") #make data look like what I'm working with
zzz
# [1] "jan feb jan mar mar jan feb"
在这个例子中,jan"出现频率最高.
I this example, "jan" occurs most frequently.
非常感谢任何建议!
推荐答案
这个怎么样:
Freq <- table(unlist(strsplit(zzz," ")))
# > Freq
# feb jan mar
# 2 3 2
> Freq[which.max(Freq)]
jan
3
如果您只想将实际单词作为输出,
If you just want the actual word as output,
> names(Freq)[which.max(Freq)]
[1] "jan"
这篇关于在字符串中查找最常见的单词的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文