What is the fastest way to compute the number of occurrences for each unique element in a vector in R?
So far, I've tried the following five functions:
f1 <- function(x)
{
aggregate(x, by=list(x), FUN=length)
}
f2 <- function(x)
{
r <- rle(x)
aggregate(r$lengths, by=list(r$values), FUN=sum)
}
f3 <- function(x)
{
u <- unique(x)
data.frame(Group=u, Counts=vapply(u, function(y)sum(x==y), numeric(1)))
}
f4 <- function(x)
{
r <- rle(x)
u <- unique(r$values)
data.frame(Group=u, Counts=vapply(u, function(y)sum(r$lengths[r$values==y]), numeric(1)))
}
f5 <- function(x)
{
as.data.frame(unclass(rle(sort(x))))[,2:1]
}
Some of them do not give the result sorted by category, but that is not important. Here are the results(used package microbenchmark
):
> x <- sample(1:100, size=1e3, TRUE); microbenchmark(f1(x), f2(x), f3(x), f4(x), f5(x))
Unit: microseconds
expr min lq median uq max neval
f1(x) 4133.353 4230.3700 4272.5985 4394.1895 7038.420 100
f2(x) 4464.268 4549.8180 4615.3465 4728.1995 7457.435 100
f3(x) 1032.064 1063.0080 1091.7670 1135.4525 3824.279 100
f4(x) 4748.950 4801.3725 4861.2575 4947.3535 7831.308 100
f5(x) 605.769 696.9615 714.9815 729.5435 3411.817 100
>
> x <- sample(1:100, size=1e4, TRUE); microbenchmark(f1(x), f2(x), f3(x), f4(x), f5(x))
Unit: milliseconds
expr min lq median uq max neval
f1(x) 25.057491 25.739892 25.937021 26.321998 27.875918 100
f2(x) 27.223552 27.718469 28.023355 28.537022 30.584403 100
f3(x) 5.361635 5.458289 5.537650 5.657967 8.261243 100
f4(x) 35.341726 35.841922 36.299161 38.012715 70.096613 100
f5(x) 2.158415 2.248881 2.281826 2.384304 4.793000 100
>
> x <- sample(1:100, size=1e5, TRUE); microbenchmark(f1(x), f2(x), f3(x), f4(x), f5(x), times=10)
Unit: milliseconds
expr min lq median uq max neval
f1(x) 236.53630 240.93358 242.88631 244.33994 250.75403 10
f2(x) 261.03280 263.61096 264.67032 265.81852 297.92244 10
f3(x) 53.94873 55.59020 59.05662 61.05741 87.23288 10
f4(x) 385.10217 390.44888 396.40572 399.23762 432.47262 10
f5(x) 18.31358 18.53492 18.84327 20.22700 20.34385 10
>
> x <- sample(1:100, size=1e6, TRUE); microbenchmark(f1(x), f2(x), f3(x), f4(x), f5(x), times=3)
Unit: milliseconds
expr min lq median uq max neval
f1(x) 2559.0462 2568.7480 2578.4498 2693.3116 2808.1734 3
f2(x) 2833.2622 2881.9241 2930.5860 2946.7877 2962.9895 3
f3(x) 743.6939 748.3331 752.9723 778.9532 804.9341 3
f4(x) 4471.8494 4544.6490 4617.4487 4696.2698 4775.0909 3
f5(x) 243.8903 253.2481 262.6058 269.1038 275.6018 3
>
> x <- sample(1:1000, size=1e6, TRUE); microbenchmark(f1(x), f2(x), f3(x), f4(x), f5(x), times=3)
Unit: milliseconds
expr min lq median uq max neval
f1(x) 2614.7104 2634.9312 2655.1520 2701.6216 2748.0912 3
f2(x) 3038.0353 3116.7499 3195.4645 3197.7423 3200.0202 3
f3(x) 6488.7268 6508.6495 6528.5722 6836.9738 7145.3754 3
f4(x) 40244.5038 40653.2633 41062.0229 41200.1973 41338.3717 3
f5(x) 244.2052 245.0331 245.8609 273.3307 300.8006 3
> x <- sample(1:10000, size=1e6, TRUE); microbenchmark(f1(x), f2(x), f3(x), f4(x), f5(x), times=3) # SLOW!
Unit: milliseconds
expr min lq median uq max neval
f1(x) 3279.2146 3300.7527 3322.2908 3338.6000 3354.9091 3
f2(x) 3563.5244 3578.3302 3593.1360 3597.2246 3601.3132 3
f3(x) 61303.6299 61928.4064 62553.1830 63089.5225 63625.8621 3
f4(x) 398792.7769 400346.2250 401899.6732 490921.6791 579943.6850 3
f5(x) 261.1835 263.7766 266.3697 287.3595 308.3494 3
(The last comparison is really slow, takes a couple minutes to run).
Apparently, the winner is f5
, but I'd like to see if it can be outperformed...
EDIT: Considering suggestions f6
by @eddi, f8
by @AdamHyland (modified) and f9
by @dickoa, here are the new results:
f6 <- function(x)
{
data.table(x)[, .N, keyby = x]
}
f8 <- function(x)
{
fac <- factor(x)
data.frame(x = levels(fac), freq = tabulate(as.integer(fac)))
}
f9 <- plyr::count
Results:
> x <- sample(1:1e4, size=1e6, TRUE); microbenchmark(f5(x), f6(x), f8(x), f9(x), times=10)
Unit: milliseconds
expr min lq median uq max neval
f5(x) 291.8189 292.69771 293.2349 293.91216 296.3622 10
f6(x) 96.5717 96.73662 96.8249 99.25542 150.1081 10
f8(x) 659.3281 663.85092 669.6831 672.43613 699.4790 10
f9(x) 284.2978 296.41822 301.3535 331.92510 346.5567 10
> x <- sample(1:1e3, size=1e7, TRUE); microbenchmark(f5(x), f6(x), f8(x), f9(x), times=10)
Unit: milliseconds
expr min lq median uq max neval
f5(x) 3190.2555 3224.4201 3264.415 3359.823 3464.782 10
f6(x) 980.1287 989.9998 1051.559 1056.484 1085.580 10
f8(x) 5092.5847 5142.3289 5167.101 5244.400 5348.513 10
f9(x) 2799.6125 2843.1189 2881.734 2977.116 3081.437 10
So data.table
is the winner! - so far :-)
p.s. I had to modify f8
to allow inputs like c(5,2,2,10)
, where not all integer from 1
to max(x)
are present.
See Question&Answers more detail:
os