複数の関数を使用してデータを集計しようとしています。出力がそれが言うものではないことを除いて、それはうまく機能します。
df<-data.frame(PLTID=rep(paste(letters), 5), R=rnorm(130, 150, 40), G=rnorm(130, 100, 50), B=rnorm(130, 200, 25))
agg.data<-aggregate(data=df, . ~ PLTID, FUN = function(x) c(mean=mean(x, na.rm=TRUE), sd=sd(x, na.rm=TRUE), n=length(x)))
> head(agg.data)
PLTID R.mean R.sd R.n G.mean G.sd G.n B.mean B.sd B.n
1 a 144.29202 28.49934 5.00000 87.85852 76.28156 5.00000 192.230731 29.349837 5.000000
2 b 148.41993 31.37764 5.00000 84.14367 21.59658 5.00000 224.862334 15.769459 5.000000
3 c 158.89111 17.75320 5.00000 114.40942 53.81999 5.00000 205.931132 5.790658 5.000000
4 d 153.70118 34.68649 5.00000 137.97890 54.79668 5.00000 209.587936 21.877864 5.000000
5 e 154.28002 27.33376 5.00000 124.87306 71.09103 5.00000 225.774236 17.720090 5.000000
6 f 144.08148 37.30555 5.00000 57.15275 44.68034 5.00000 204.709050 18.207047 5.000000
これは私が望むものです...後で列にアクセスしたい場合を除いて、実際にはそこにありません。
> names(agg.data)
[1] "PLTID" "R" "G" "B"
> str(agg.data)
'data.frame': 26 obs. of 4 variables:
$ PLTID: Factor w/ 26 levels "a","b","c","d",..: 1 2 3 4 5 6 7 8 9 10 ...
$ R : num [1:26, 1:3] 144 148 159 154 154 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr "mean" "sd" "n"
$ G : num [1:26, 1:3] 87.9 84.1 114.4 138 124.9 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr "mean" "sd" "n"
$ B : num [1:26, 1:3] 192 225 206 210 226 ...
..- attr(*, "dimnames")=List of 2
.. ..$ : NULL
.. ..$ : chr "mean" "sd" "n"
> sessionInfo()
R version 3.0.2 (2013-09-25)
Platform: x86_64-apple-darwin10.8.0 (64-bit)
locale:
[1] en_US.UTF-8/en_US.UTF-8/en_US.UTF-8/C/en_US.UTF-8/en_US.UTF-8
attached base packages:
[1] grid stats graphics grDevices utils datasets methods base
other attached packages:
[1] data.table_1.8.10 plyr_1.8 gridExtra_0.9.1 rgl_0.93.963 ggplot2_0.9.3.1
loaded via a namespace (and not attached):
[1] colorspace_1.2-4 dichromat_2.0-0 digest_0.6.3 gtable_0.1.2 labeling_0.2 MASS_7.3-29 munsell_0.4.2 proto_0.3-10 RColorBrewer_1.0-5
[10] reshape2_1.2.2 scales_0.2.3 stringr_0.6.2 tools_3.0.2