作者celestialgod (天)
看板R_Language
标题Re: [问题] merge 3 tables with summing common var
时间Tue Oct 13 16:21:44 2015
※ 引述《celestialgod (天)》之铭言:
: ※ 引述《celestialgod (天)》之铭言:
: : 简短但是慢很多,提供参考XD
: : 你的方法在我i5第一代电脑上测试,大概是0.36秒,下面最快方法大概是2.9秒
: : 我测了一下,主要是在group_by做和的时候比较慢
: : library(plyr)
: : library(dplyr)
: : library(tidyr)
: : library(data.table)
: : # rbind.fill是参考参考网址的
: 原本这里会出问题,稍微修改之後就正常了,可是速度会骤降
: t = proc.time()
: wide_table = rbind.fill(list(x, y, z)) %>% tbl_dt(FALSE)
: # 这行是错的,会出现NA+NA+NA = 0的情况
: # sum_without_na = function(x) sum(x, na.rm = TRUE)
: sum_without_na = function(x) ifelse(all(is.na(x)), NA_integer_,
: sum(x, na.rm = TRUE))
: out = wide_table %>% group_by(SP) %>% summarise_each(funs(sum_without_na))
: proc.time() - t # 8.66 seconds
: : # 参考下面网址的
: : t = proc.time()
: : wide_table = rbind.fill(list(x, y, z)) %>% tbl_dt(FALSE)
: : out2 = ddply(wide_table, .(SP), function(x) colSums(x, na.rm = TRUE))
: : proc.time() - t # 50 seconds
: : # 利用tidyr做的,感觉很费工~"~
: : t = proc.time()
: : out3 = list(x, y, z) %>% llply(function(x){
: : gather(x, variable, values, -SP) %>%
: : mutate(variable = as.character(variable))
: : }) %>% bind_rows %>% group_by(SP, variable) %>%
: : summarise(values = sum(values)) %>%
: : spread(variable, values)
: : proc.time() - t # 3.9 seconds
: : 参考网址:http://tinyurl.com/o7gbeej
上一篇的内容:
我把cy大的code改成dplyr
然後另外多一写一个Rcpp 去做aggregate的动作 (完整程式後面有连结)
把要加总的data.frame个别抓出来加总
原本想说可以很精简的方式完成
最後发现我写得很复杂(叹气
程式:
http://pastebin.com/Q7XEpTnb
本篇:
我终於毫无悬念的找到比较快又不会太复杂的方法....
感谢Edster大大提供了rowSums的想法,进而有了这一个方法
因为这个用了purrr,所以就另开了一篇文章...
增加30%的速度左右,因为是一次merge,而非两两merge
再补一个测试数据:
在多一个data.table w
w <- matrix(sample(2e6), 1e5) %>% data.table() %>%
setnames(1:20,sample(LETTERS,20)) %>% .[,SP:=seq_len(nrow(.))]
cy大的方法
user system elapsed
0.76 0.09 0.87
上一篇Rpp的方法
user system elapsed
0.98 0.05 1.03
rbind.fill with group_by, summarise
user system elapsed
9.06 0.04 9.22
tidyr
user system elapsed
2.16 0.27 2.48
本篇
user system elapsed
0.31 0.05 0.36
果然一起做之後,比较不会随着两两merge次数变多,而效率drop down过快
如同do.call 跟 Reduce......
好读版:
http://pastebin.com/VZisufKC
10/14 00:45新增
加一个Rcpp版本:
http://pastebin.com/ZhhiV6VS
user system elapsed
1.46 0.11 1.57
速度瓶颈在处理两两相加,跳过NA的地方
(我如果直接用armadillo的vector相加只要0.13秒)
这里只要处理好,cpp应该可以加速不少
10/14 01:23新增
应该是最後一个版本了XDD
最後用RcppArmadillo实现NA相加
user system elapsed
0.04 0.03 0.12
程式:
http://pastebin.com/piu2Qxcd
我觉得这已经很快很快了XDD
没研究下去的必要了~~~
10/14 02:20
以上结果都是在我i5-760的电脑上执行的结果
最後给整合版本的程式:
http://pastebin.com/HthDsLnz
最後程式都以整合版本为主
整合版本程式是在
[email protected]电脑上跑的结果
i7电脑的session information 如下:
Revolution R Open 3.2.2
Default CRAN mirror snapshot taken on 2015-08-27
The enhanced R distribution from Revolution Analytics
Visit mran.revolutionanalytics.com/open for information
about additional features.
Multithreaded BLAS/LAPACK libraries detected. Using 4 cores for math
algorithms.
R version 3.2.2 (2015-08-14)
Platform: x86_64-w64-mingw32/x64 (64-bit)
Running under: Windows 7 x64 (build 7601) Service Pack 1
locale:
[1] LC_COLLATE=Chinese (Traditional)_Taiwan.950
[2] LC_CTYPE=Chinese (Traditional)_Taiwan.950
[3] LC_MONETARY=Chinese (Traditional)_Taiwan.950
[4] LC_NUMERIC=C
[5] LC_TIME=Chinese (Traditional)_Taiwan.950
attached base packages:
[1] stats graphics grDevices utils datasets methods base
other attached packages:
[1] RcppArmadillo_0.5.400.2.0 Rcpp_0.12.0
[3] magrittr_1.5 purrr_0.1.0
[5] tidyr_0.2.0 dplyr_0.4.2
[7] plyr_1.8.3 data.table_1.9.4
[9] RevoUtilsMath_3.2.2
loaded via a namespace (and not attached):
[1] assertthat_0.1 chron_2.3-47 R6_2.1.1 DBI_0.3.1
[5] stringi_0.5-5 lazyeval_0.1.10 reshape2_1.4.1 tools_3.2.2
[9] stringr_1.0.0 parallel_3.2.2
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.109.73.190
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1444724507.A.2BF.html
1F:→ cywhale: 厉害,这麽多新名词 哈哈 真是很好的练习范本..非常感谢 10/13 19:16
2F:→ celestialgod: purrr 我刚好最近在练... 这也是我的练习范本,哈 10/13 19:18
3F:→ celestialgod: 哈哈 10/13 19:18
4F:推 cywhale: 我下班前在公司还跑不出来,套件可能有版本问题,家中的 10/13 19:24
5F:→ cywhale: 就可以,另外两台电脑跑Rcpp的microbenchmark差不多,但 10/13 19:25
6F:→ cywhale: dplyr版本则不同电脑差很多,purrr很快但在公司跑不出来~ 10/13 19:26
7F:→ celestialgod: purrr的版本在三个df,不会差太多 10/13 19:27
8F:→ celestialgod: 三个以上,两两merge变多,速度差距就出来了 10/13 19:27
9F:→ celestialgod: 我记得purrr只有0.1.0版本,你可能要确定r版本,3.1 10/13 19:28
10F:→ celestialgod: 跟3 10/13 19:28
11F:→ celestialgod: 2之间有些程式会有问题 10/13 19:28
※ 编辑: celestialgod (180.218.154.163), 10/14/2015 02:27:09
12F:推 cywhale: 最好版本好威 armadillo学习范本get..问一问题收益良多! 10/14 08:52
13F:推 cywhale: 最後(..打错)...这些版本收藏了!感谢c大费心整理! 10/14 09:00