计算R中向量中每个唯一元素出现次数的最快方法是什么?
到目前为止,我已经尝试了以下五个功能:
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]
}
他们中的一些人没有按类别给出结果,但这并不重要。以下是结果(使用包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
(最后的比较非常慢,需要几分钟才能运行)。
显然,获胜者是f5
,但我想看看它是否能超越......
编辑:考虑@eddi的建议f6
,@ AdamHyland的f8
(已修改)和@dickoa的f9
,以下是新的结果:
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
结果:
> 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
所以data.table
是赢家! - 到目前为止: - )
P.S。我必须修改f6
以允许输入c(5,2,2,10)
,其中不存在从1
到max(x)
的所有整数。
答案 0 :(得分:13)
这比tabulate
慢一点,但更通用(它可以用于字符,因子,基本上不管你扔什么),更容易阅读/维护/扩展。
library(data.table)
f6 = function(x) {
data.table(x)[, .N, keyby = x]
}
x <- sample(1:1000, size=1e7, TRUE)
system.time(f6(x))
# user system elapsed
# 0.80 0.07 0.86
system.time(f8(x)) # tabulate + dickoa's conversion to data.frame
# user system elapsed
# 0.56 0.04 0.60
更新:自data.table
版本1.9.3起,data.table
版本实际上比tabulate
+ data.frame
转换速度快约2倍。
答案 1 :(得分:11)
如果您能够满足初始条件,几乎没有什么可以击败tabulate()
。
x <- sample(1:100, size=1e7, TRUE)
system.time(tabulate(x))
# user system elapsed
# 0.071 0.000 0.072
@dickoa在评论中添加了一些关于如何获得适当输出的注释,但列表作为主力函数是要走的路。