Find the n most common values in a vector

RCountRanking

R Problem Overview


I have a vector say

c(1,1,1,1,1,1,2,3,4,5,7,7,5,7,7,7)

How do I count each element, and then return the e.g. 3 most common elements, i.e. 1, 7, 5?

R Solutions


Solution 1 - R

I'm sure this is a duplicate, but the answer is simple:

sort(table(variable),decreasing=TRUE)[1:3]

Solution 2 - R

I don't know if this is better than the table approach, but if your list is already a factor then its summary method will give you frequency counts:

> summary(as.factor(c(1,1,1,1,1,1,2,3,4,5,7,7,5,7,7,7)))
1 2 3 4 5 7 
6 1 1 1 2 5 

And then you can get the top 3 most frequent like so:

> names(sort(summary(as.factor(c(1,1,1,1,1,1,2,3,4,5,7,7,5,7,7,7))), decreasing=T)[1:3])
[1] "1" "7" "5"

Solution 3 - R

If your vector contains only integers, tabulate will be much faster than anything else. There are a couple of catches to be aware of:

  • It'll by default return the count for numbers from 1 to N.
  • It'll return an unnamed vector.

That means, if your x = c(1,1,1,3) then tabulate(x) will return (3, 0, 1). Note that the counts are for 1 to max(x) by default.

How can you use tabulate to make sure that you can pass any numbers?

set.seed(45)
x <- sample(-5:5, 25, TRUE)
#  [1]  1 -2 -3 -1 -2 -2 -3  1 -3 -5 -1  4 -2  0 -1 -1  5 -4 -1 -3 -4 -2  1  2  4

Just add abs(min(x))+1 when min(x) <= 0 to make sure that the values start from 1. If min(x) > 0, then just use tabulate directly.

sort(setNames(tabulate(x + ifelse(min(x) <= 0, abs(min(x))+1, 0)), 
      seq(min(x), max(x))), decreasing=TRUE)[1:3]

If your vector does contain NA, then you can use table with useNA="always" parameter.

Solution 4 - R

you can use table() function to get a tabulation of the frequency of values in an array/vector and then sort this table.

x = c(1, 1, 1, 2, 2)
sort(table(x))
2 1
2 3

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionChairmanMeowView Question on Stackoverflow
Solution 1 - RThomasView Answer on Stackoverflow
Solution 2 - RqwwqwwqView Answer on Stackoverflow
Solution 3 - RArunView Answer on Stackoverflow
Solution 4 - RcobieView Answer on Stackoverflow