Order unique values by frequency

若如初见. 提交于 2019-12-01 20:43:53

You can sort the tabled values in decreasing order and then take the names to get the output you're looking for. Try this:

> x <- c("apple", "banana", "apple", "banana", "banana", 
         "desk", "pen", "pen", "pen", "pen")
> names(sort(table(x), decreasing = TRUE))
## [1] "pen"    "banana" "apple"  "desk"  

table is intuitive for base R. Here's a qdap approach:

library(qdap)
freq_terms(dat, 4)

##   WORD   FREQ
## 1 pen       4
## 2 banana    3
## 3 apple     2
## 4 desk      1

Or...

freq_terms(dat, 4)[, 1]
## [1] "pen"    "banana" "apple"  "desk"

An efficient way to do this would be:

k <- {tmp <- table(x); names(tmp)[order(tmp, decreasing = TRUE)]}
k
[1] "pen"    "banana" "apple"  "desk" 
标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!