dplyr最強伝説
plyrは遅いと思っておりましたし実際そのような検証結果も2013のR Advent calendarで出ておりました。
まあ文法がggplot2と同じような感じなのでggplot脳の自分としてはまあ多少遅くても新しい文法覚えるよりましかとか思っていたわけです。
ただ、新しい文法覚えて集計がめちゃくちゃ速くなるんなら話は別だ。
ということで巷でめちゃくちゃ速いと評判のdplyrを使って集計時間を比較してみました。
実際速い。
追記(2014/1/19)
CRANにdplyrがアップされていたので、githubからインストールする必要は無くなりました。
通常通りinstall.packagesでどうぞ。
追記(2014/2/24)
基本関数等については下記記事をご参照ください。
http://rpubs.com/dichika/dplyr_intro
http://rpubs.com/dichika/dplyr_db
> # dplyrはgithub上からインストール > #library(devtools) > #install_github("dplyr") > > library(plyr) > library(doBy) > library(dplyr) > > set.seed(42) > > # テストデータ作成 > types <- c("A", "B", "C", "D", "E", "F") > obs <- 4e+06 > dat <- data.frame(type = as.factor(sample(types, obs, replace = TRUE)), + value = round(runif(obs, min = 0, max = 1), digits = 2) + ) > > print(object.size(dat), units = "MB") 45.8 Mb > > > # 集計 > plyr_time <- system.time(plyr_res <- ddply(dat, .(type), summarize, + mean_percent = mean(value)) + ) > > doBy_time <- system.time(doBy_res <- summaryBy(value ~ type, + data = dat, + FUN = mean, + keep.names = TRUE) + ) > > aggregate_time <- system.time(aggregate_res <- aggregate(value~type, data=dat, mean) + ) > > dplyr_time <- system.time(dplyr_res <- summarise(group_by(dat, type), + mean(value)) + ) > > > # 結果の確認 > plyr_res type mean_percent 1 A 0.4998437 2 B 0.4999486 3 C 0.4995219 4 D 0.4998195 5 E 0.5002907 6 F 0.5004802 > doBy_res type value 1 A 0.4998437 2 B 0.4999486 3 C 0.4995219 4 D 0.4998195 5 E 0.5002907 6 F 0.5004802 > aggregate_res type value 1 A 0.4998437 2 B 0.4999486 3 C 0.4995219 4 D 0.4998195 5 E 0.5002907 6 F 0.5004802 > dplyr_res Source: local data frame [6 x 2] type mean(value) 1 C 0.4995219 2 A 0.4998437 3 D 0.4998195 4 E 0.5002907 5 B 0.4999486 6 F 0.5004802 > > # 集計時間の確認 > plyr_time ユーザ システム 経過 1.837 0.456 2.303 > doBy_time ユーザ システム 経過 3.876 0.307 4.219 > aggregate_time ユーザ システム 経過 8.008 0.447 8.534 > dplyr_time ユーザ システム 経過 0.311 0.049 0.360 > sessionInfo() R version 3.0.2 (2013-09-25) Platform: x86_64-apple-darwin10.8.0 (64-bit) locale: [1] ja_JP.UTF-8/ja_JP.UTF-8/ja_JP.UTF-8/C/ja_JP.UTF-8/ja_JP.UTF-8 attached base packages: [1] splines stats graphics grDevices utils datasets methods [8] base other attached packages: [1] devtools_1.4.1 dplyr_0.1 doBy_4.5-10 MASS_7.3-29 [5] survival_2.37-4 plyr_1.8 loaded via a namespace (and not attached): [1] assertthat_0.1 digest_0.6.4 evaluate_0.5.1 grid_3.0.2 [5] httr_0.2 lattice_0.20-23 lme4_1.0-5 Matrix_1.1-1.1 [9] memoise_0.1 minqa_1.2.2 nlme_3.1-111 parallel_3.0.2 [13] Rcpp_0.10.6 RCurl_1.95-4.1 stringr_0.6.2 tools_3.0.2 [17] whisker_0.3-2