Find the n most common values in a vector [duplicate]
Solution 1:
I'm sure this is a duplicate, but the answer is simple:
sort(table(variable),decreasing=TRUE)[1:3]
Solution 2:
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:
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:
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