Re: [問題] merge 3 tables with summing common var
※ 引述《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
最後程式都以整合版本為主
整合版本程式是在i7-3770K@4.4GHz電腦上跑的結果
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://www.ptt.cc/bbs/R_Language/M.1444724507.A.2BF.html
→
10/13 19:16, , 1F
10/13 19:16, 1F
→
10/13 19:18, , 2F
10/13 19:18, 2F
→
10/13 19:18, , 3F
10/13 19:18, 3F
推
10/13 19:24, , 4F
10/13 19:24, 4F
→
10/13 19:25, , 5F
10/13 19:25, 5F
→
10/13 19:26, , 6F
10/13 19:26, 6F
→
10/13 19:27, , 7F
10/13 19:27, 7F
→
10/13 19:27, , 8F
10/13 19:27, 8F
→
10/13 19:28, , 9F
10/13 19:28, 9F
→
10/13 19:28, , 10F
10/13 19:28, 10F
→
10/13 19:28, , 11F
10/13 19:28, 11F
※ 編輯: celestialgod (180.218.154.163), 10/14/2015 02:27:09
推
10/14 08:52, , 12F
10/14 08:52, 12F
推
10/14 09:00, , 13F
10/14 09:00, 13F
討論串 (同標題文章)
完整討論串 (本文為第 5 之 5 篇):
R_Language 近期熱門文章
PTT數位生活區 即時熱門文章