Find most common word in a character string

岁酱吖の 提交于 2021-02-08 09:17:38

问题


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

Example code:

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"

I this example, "jan" occurs most frequently.

Any suggestions are greatly appreciated!


回答1:


How about this:

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"



回答2:


You could also factor the split vector then tabulate.

f <- factor(strsplit(zzz, " ")[[1]])
levels(f)[which.max(tabulate(f))]
# [1] "jan"


来源:https://stackoverflow.com/questions/26455294/find-most-common-word-in-a-character-string

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!