作者cywhale (cywhale)
看板R_Language
标题[问题] merge 3 tables with summing common var
时间Mon Oct 12 16:54:47 2015
[问题类型]:
效能谘询(我想让R 跑更快)
好像在哪曾看过较简易的写法或function,但一时想不起,也没找到,写了比较复杂的
code,想请问是否有更快或更简易的方式做到
[软体熟悉度]:
请把以下不需要的部份删除
入门(写过其他程式,只是对语法不熟悉)
[问题叙述]:
请简略描述你所要做的事情,或是这个程式的目的
Merge some data tables by the same key, 但若有相同的variables则合并时要相加,
不管NA,data tables彼此间的行、列数均不同
[程式范例]:
library(data.table)
library(dplyr)
# testing data, assuming merge by key = "SP"
set.seed(NULL)
x <- matrix(sample(1e6), 1e5) %>% data.table() %>%
setnames(1:10,sample(LETTERS,10)) %>% .[,SP:=seq_len(nrow(.))]
y <- matrix(sample(1e5), 1e4) %>% data.table() %>%
setnames(1:10,sample(LETTERS,10)) %>% .[,SP:=seq_len(nrow(.))]
z <- matrix(sample(4e5), 2e4) %>% data.table() %>%
setnames(1:20,sample(LETTERS,20)) %>% .[,SP:=seq_len(nrow(.))]
# function.. try to write Rcpp function..
require(Rcpp)
cppFunction('NumericVector addv(NumericVector x, NumericVector y) {
NumericVector out(x.size());
NumericVector::iterator x_it,y_it,out_it;
for (x_it = x.begin(), y_it=y.begin(), out_it = out.begin();
x_it != x.end(); ++x_it, ++y_it, ++out_it) {
if (ISNA(*x_it)) {
*out_it = *y_it;
} else if (ISNA(*y_it)) {
*out_it = *x_it;
} else {
*out_it = *x_it + *y_it;
}
}
return out;}')
### merge two data.table with different columns/rows,
### and summing identical column names
outer_join2 <- function (df1,df2,byNames) {
tt=intersect(colnames(df1)[-match(byNames,colnames(df1))],
colnames(df2)[-match(byNames,colnames(df2))])
df <- merge(df2,df1[,-tt,with=F],by=byNames,all=T)
dt <- merge(df2[,-tt,with=F],df1[,c(byNames,tt),with=F],by=byNames,all=T) %>%
.[,tt,with=F]
for (j in colnames(dt)) {set(df,j=j,value=addv(df[[j]],dt[[j]]))}
return (df)
}
# get results, 参考c大
#1LaHm_aH (R_Language)
system.time(Reduce(function(x, y) outer_join2(x, y, byNames="SP"), list(x,y,z)))
用了较多行code来完成这件事,速度上似乎还可以,但不确定是否有更好的写法?谢谢!
[关键字]:
选择性,也许未来有用
--
※ 发信站: 批踢踢实业坊(ptt.cc), 来自: 140.112.65.48
※ 文章网址: https://webptt.com/cn.aspx?n=bbs/R_Language/M.1444640089.A.EE0.html
1F:推 celestialgod: 本系列收录於z-4-13-5 10/15 17:40
2F:→ cywhale: I saw it in 'z-4-13-5' not 14 10/15 21:51
3F:→ celestialgod: cy大对,我错了 我可能当时太急着要下班XDD 10/15 21:57
4F:推 celestialgod: 可以麻烦cy大帮我修推文吗 避免後面的人看到我错误 10/15 21:58
5F:→ celestialgod: 的指示,而找不到 10/15 21:58
※ 编辑: cywhale (36.228.159.121), 10/15/2015 22:07:23
6F:→ cywhale: OK, done ^_^ 10/15 22:08
7F:推 celestialgod: 谢谢!! 10/15 22:08