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.