Tea90
Tea90

Reputation: 33

R: sort words by the frequency in which they occur

This seems like a simple question but I can't figure it out, I am trying to sort the occurrence of words in a vector by their frequency.

For example I have tried

x = 'it was a warm and sunny day it was a great day'
x = unlist(strsplit(x,' '))
unlist(sapply(x,sort,decreasing=T))

However, this only seems to sort the words in the order they occur. Any help would be much appreciated

Upvotes: 3

Views: 4192

Answers (1)

Thomas
Thomas

Reputation: 44535

As @akrun notes in a comment, you may want a table:

sort(table(x), decreasing=TRUE) 
## x
##     a   day    it   was   and great sunny  warm 
##     2     2     2     2     1     1     1     1 

Or you may want to just have a vector of the values in the order of frequency:

names(sort(table(x), decreasing=TRUE))
## [1] "a"     "day"   "it"    "was"   "and"   "great" "sunny" "warm"

Or maybe you want the original vector sorted with each original element included, so something like:

rep(names(sort(table(x), decreasing=TRUE)), sort(table(x), decreasing=TRUE))
##  [1] "a"     "a"     "day"   "day"   "it"    "it"    "was"   "was"   "and"   "great" "sunny" "warm" 

Upvotes: 6

Related Questions