1

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

示例数据:

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最近几天一直在转来转去,但没有到达任何地方。帮助表示赞赏!

4

2 回答 2

3

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

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
于 2012-07-24T05:50:45.337 回答
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
于 2012-07-24T09:10:21.420 回答