dplyrのjoinがmergeに比べて3倍速い
私、集計はplyrでやろうぜええええええええええとか言ってる割に、データマージはmerge()でやっておりました。
もうそろそろ冬休み終わるし沢山の集計作業が待っているが俺は早く帰りたい。
ということでdplyrの*_join()のうち、inner_join()を試してみました。
3倍速い。
これからはinner_join使います。
なお、本当はplyrのjoinとも比較したかったんだけど、なぜかRが落ちるのでやめました。
追記
ヘルプを読むと、outputのrenameとか変数名が異なるものをbyに指定した結合とかはできないとのことなので注意。
Unlike merge, preserves the order of x no matter what join type is used. If needed, rows from y will be added to the bottom. Join is often faster than merge, although it is somewhat less featureful - it currently offers no way to rename output or merge on different variables in the x and y data frames.
> library(dplyr) 次のパッケージを付け加えます: ‘dplyr’ 以下のオブジェクトはマスクされています (from ‘package:stats’) : filter, lag 以下のオブジェクトはマスクされています (from ‘package:base’) : intersect, setdiff, setequal, union 警告メッセージ: '.path.package' は廃止予定です 'path.package' を代わりに使って下さい help("Deprecated") を見て下さい > set.seed(42) > > # テストデータ作成 > types <- c("A", "B", "C", "D", "E", "F") > obs <- 4e+06 > dat <- data.frame(id=seq(obs), + type= as.factor(sample(types, obs, replace = TRUE)), + value = round(runif(obs, min = 0, max = 1), digits = 2) + ) > dat2 <- data.frame(id=seq(obs), + type2= as.factor(sample(types, obs, replace = TRUE)), + value2 = round(runif(obs, min = 0, max = 1), digits = 2) + ) > > print(object.size(dat), units = "MB") 61 Mb > print(object.size(dat2), units = "MB") 61 Mb > > time_merge <- system.time(res_merge <- merge(dat, dat2, by="id")) > time_dplyr <- system.time(res_dplyr <- inner_join(dat, dat2, by="id")) > > time_merge ユーザ システム 経過 22.888 1.243 25.953 > time_dplyr ユーザ システム 経過 7.244 0.548 8.020 > 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] stats graphics grDevices utils datasets methods base other attached packages: [1] dplyr_0.1 loaded via a namespace (and not attached): [1] assertthat_0.1 Rcpp_0.10.6 tools_3.0.2