试图成为基准dplyr与data.table

时间:2019-07-18 10:30:12

标签: r dplyr data.table

为什么此代码不起作用?我该如何对这些表达式进行基准测试?

library(data.table)
library(dplyr)
dt <- as.data.table(mtcars) 

(lb <- bench::mark(
  dt[, .N, by = .(am, gear) ],
  count(dt, am, gear)
))
  

all.equal.data.table(results $ result [[1]],results $ result [[i]])中的错误:             “目标”和“当前”都必须是data.tables

1 个答案:

答案 0 :(得分:0)

在这种情况下,microbenchmark软件包将非常有效。

library(data.table)
library(dplyr)
library(microbenchmark)

dt <- as.data.table(mtcars) 

microbenchmark::microbenchmark(
  dt = dt[, .N, by = .(am, gear) ],
  dplyr = count(dt, am, gear)
)

# Unit: microseconds
#   expr     min       lq      mean   median        uq       max neval
#     dt 366.895  441.917  666.3117  471.690  545.9255  8154.319   100
#  dplyr 934.658 1049.023 1649.7788 1144.242 1255.5120 29170.144   100