Grouping ecological data in R

前提是你 提交于 2019-12-06 08:36:49

问题


I'm looking at some ecological data (diet) and trying to work out how to group by Predator. I would like to be able to extract the data so that I can look at the weights of each individual prey for each species for each predator, i.e work out the mean weight of each species eaten by e.g Predator 117. I've put a sample of my data below.

   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

回答1:


You can use the aggregate function as follows:

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



回答2:


There are a few different ways of getting what you want:

  1. The aggregate function. Probably what you are after.

    aggregate(PreyWeight ~ Predator + PreySpecies, data=dd, FUN=mean)
    
  2. tapply: Very useful, but only divides the variable by a single factor, hence, we need to create a need joint factor with the paste command:

    tapply(dd$PreyWeight, paste(dd$Predator, dd$PreySpecies), mean)
    
  3. ddply: Part of the plyr package. Very useful. Worth learning.

    require(plyr)
    ddply(dd, .(Predator, PreySpecies), summarise, mean(PreyWeight))
    
  4. dcast: The output is in more of a table format. Part of the reshape2 package.

    require(reshape2)
    dcast(dd, PreyWeight ~ PreySpecies+ Predator, mean, fill=0)
    



回答3:


mean(data$PreyWeight[data$Predator==117]);



来源:https://stackoverflow.com/questions/10047124/grouping-ecological-data-in-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!