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

时间:2012-07-24 05:45:55

标签: r aggregate reshape

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

示例数据:

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一起回转,但没有到达任何地方。帮助赞赏!

2 个答案:

答案 0 :(得分:3)

我可能会使用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

答案 1 :(得分:1)

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

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