繁体   English   中英

来自reshape2的dcast中的“复杂”聚合函数

[英]'Complex' aggregation function in dcast from reshape2

我有一个长格式的数据框,我需要汇总特定日期的几个观察结果。

示例数据:

long <- structure(list(Day = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 
2L, 2L, 2L, 2L), .Label = c("1", "2"), class = "factor"), 
Genotype = structure(c(1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L, 
2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), View = structure(c(1L, 
2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L, 1L, 2L, 3L), .Label = c("1", 
"2", "3"), class = "factor"), variable = c(1496L, 1704L, 
1738L, 1553L, 1834L, 1421L, 1208L, 1845L, 1325L, 1264L, 1920L, 
1735L)), .Names = c("Day", "Genotype", "View", "variable"), row.names = c(NA, -12L),
class = "data.frame")

> long
   Day Genotype View variable
1    1        A    1     1496
2    1        A    2     1704
3    1        A    3     1738
4    1        B    1     1553
5    1        B    2     1834
6    1        B    3     1421
7    2        A    1     1208
8    2        A    2     1845
9    2        A    3     1325
10   2        B    1     1264
11   2        B    2     1920
12   2        B    3     1735

我需要通过获取每个视图的产品的立方根来聚合每天的每个基因型。 因此对于第1天的基因型A, (1496 * 1704 * 1738)^(1/3) 最终的数据框架如下所示:

  Day Genotype  summary
1   1        A 1642.418
2   1        B 1593.633
3   2        A 1434.695
4   2        B 1614.790

过去几天一直在和reshape2一起reshape2 ,但没有到达任何地方。 帮助赞赏!

我可能会使用plyrddply来完成这项任务:

library(plyr)

ddply(long, .(Day, Genotype), summarize, 
      summary = prod(variable) ^ (1/3))
#-----
  Day Genotype  summary
1   1        A 1642.418
2   1        B 1593.633
3   2        A 1434.695
4   2        B 1614.790

或者用dcast

dcast(data = long, Day + Genotype ~ ., 
      value.var = "variable", function(x) prod(x) ^ (1/3))
#-----
  Day Genotype       NA
1   1        A 1642.418
2   1        B 1593.633
3   2        A 1434.695
4   2        B 1614.790

没有其他包的其他解决方案。

aggregate(list(Summary=long$variable),by=list(Day=long$Day,Genotype=long$Genotype),function(x) prod(x)^(1/length(x)))
  Day Genotype  Summary
1   1        A 1642.418
2   2        A 1434.695
3   1        B 1593.633
4   2        B 1614.790

暂无
暂无

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

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