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 に答える