我正在查看一些生态数据(饮食),并试图弄清楚如何按Predator进行分组。我希望能够提取数据,以便我可以查看每个捕食者的每个物种的每个猎物的权重,即计算出例如捕食者117吃的每个物种的平均重量。我在下面放了一个我的数据样本。
Predator PreySpecies PreyWeight
1 114 10 4.2035496
2 114 10 1.6307026
3 115 1 407.7279775
4 115 1 255.5430495
5 117 10 4.2503708
6 117 10 3.6268814
7 117 10 6.4342073
8 117 10 1.8590861
9 117 10 2.3181421
10 117 10 0.9749844
11 117 10 0.7424772
12 117 15 4.2803743
13 118 1 126.8559155
14 118 1 276.0256158
15 118 1 123.0529734
16 118 1 427.1129793
17 118 3 237.0437606
18 120 1 345.1957190
19 121 1 160.6688815发布于 2012-04-07 02:20:26
您可以按如下方式使用aggregate函数:
aggregate(formula = PreyWeight ~ Predator + PreySpecies, data = diet, FUN = mean)
# Predator PreySpecies PreyWeight
# 1 115 1 331.635514
# 2 118 1 238.261871
# 3 120 1 345.195719
# 4 121 1 160.668881
# 5 118 3 237.043761
# 6 114 10 2.917126
# 7 117 10 2.886593
# 8 117 15 4.280374发布于 2012-04-07 04:01:11
有几种不同的方法可以获得你想要的东西:
aggregate函数。可能就是你要找的东西。aggregate( FUN=mean)
tapply:~PreyWeight+ PreySpecies,data=dd,aggregate非常有用,但只将变量除以一个因子,因此,我们需要使用粘贴命令创建一个需要的联合因子:tapply(dd$PreyWeight,paste(dd$Predator,dd$PreySpecies),plyr包的mean)
ddply:部分。非常有用。值得学习。require(plyr) ddply(dd,.(Predator,PreySpecies),summarise,mean(PreyWeight))
dcast:)输出更多的是表格格式。reshape2包的一部分。需要(Reshape2) dcast(dd,PreyWeight ~ PreySpecies+ Predator,mean,fill=0)
发布于 2012-04-11 04:51:29
mean(data$PreyWeight[data$Predator==117]);
https://stackoverflow.com/questions/10047124
复制相似问题