简体   繁体   English

R data.table:重用聚合

[英]R data.table: reuse an aggregation

I want to apply the same aggregation to multiple data tables, without rewriting the aggregation scheme. 我想将相同的聚合应用于多个数据表,而不重写聚合方案。

Consider 考虑

dt1 <- data.table(id = c(1,2), a = rnorm(10), b = rnorm(10), c = rnorm(10))
dt2 <- data.table(id = c(1,2), a = rnorm(10), b = rnorm(10), c = rnorm(10))

dt1_aggregates <- dt1[, .(mean_a=mean(a), sd_a=sd(a), mean_b=mean(b), sd_b=sd(b)), by=id]
dt2_aggregates <- dt2[, .(mean_a=mean(a), sd_a=sd(a), mean_b=mean(b), sd_b=sd(b)), by=id]

Is there some way to reuse the dt1_aggregates aggregation scheme for dt2 without having to write it out twice? 有没有办法为dt2重用dt1_aggregates聚合方案而不必将其写出两次?

You can quote the expression you want, and then evaluate it within the data.table: 您可以引用所需的表达式,然后在data.table中对其进行评估:

my.call=quote(list(mean_a=mean(a), sd_a=sd(a), mean_b=mean(b), sd_b=sd(b)))
dt1[, eval(my.call), by=id]

Produces 产生

   id       mean_a      sd_a      mean_b      sd_b
1:  1  0.004165423 0.7504691 -0.05001424 1.4440434
2:  2 -0.430910188 0.9648096  0.26918995 0.8680997

And

dt2[, eval(my.call), by=id]

Produces 产生

   id     mean_a     sd_a     mean_b      sd_b
1:  1  0.2974145 1.191863 -0.0588854 0.7896988
2:  2 -0.4642856 1.438937  0.3612607 1.0581702

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM