17

计算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,但我想看看它是否可以超越...


编辑:考虑f6@eddi、f8@AdamHyland(修改)和f9@dickoa 的建议,以下是新结果:

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也一样!- 至今 :-)

ps 我必须修改f8以允许输入c(5,2,2,10),其中并非所有整数1max(x)存在。

4

2 回答 2

14

这比 慢一点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.table1.9.3 版开始,该data.table版本实际上比tabulate+data.frame转换快 2 倍。

于 2013-06-20T20:54:28.740 回答
13

tabulate()只要你能满足初始条件,几乎没有什么可以打败的。

x <- sample(1:100, size=1e7, TRUE)
system.time(tabulate(x))
#  user  system elapsed 
# 0.071   0.000   0.072 

@dickoa 在评论中添加了一些关于如何获得适当输出的注释,但制表作为主要功能是要走的路。

于 2013-06-20T20:48:53.020 回答