3

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
joran
  • 169,992
  • 32
  • 429
  • 468
Luke
  • 33
  • 5
  • possible duplicate of [Calculating the mean of values in tables using formulae \[R\]](http://stackoverflow.com/questions/6798327/calculating-the-mean-of-values-in-tables-using-formulae-r) – mechanical_meat Apr 08 '12 at 03:33

3 Answers3

7

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
flodel
  • 87,577
  • 21
  • 185
  • 223
5

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)
    
csgillespie
  • 59,189
  • 14
  • 150
  • 185
0

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

flies
  • 2,017
  • 2
  • 24
  • 37
  • This is probably not quite as practically useful as the other answers given here ... if you wanted to get the results for all predator species, you'd need a whole series of statements like this, or a for loop ... more useful (I think) as a pedagogical example. – Ben Bolker Apr 10 '12 at 20:59