I have a large dataframe in the same format as d below, with count data for three columns nested within two different factors.
elevation   distance    sp1 sp2 spn
1500    0   2   2   5
1500    0   2   1   5
1500    50  2   2   5
1500    50  2   2   6
2000    0   9   2   5
2000    0   7   2   2
2000    50  4   3   6
2000    50  4   3   4
Notice that there are two replicate rows for each level of factor d$distance.
I would like to aggregate those replicate rows for each distance level within each elevation by summing in each column, so it ends up like this:
elevation   distance    sp1 sp2 spn
1500    0   4   3   10
1500    50  4   4   11
2000    0   16  4   7
2000    50  8   6   10
I can do it easily for one column, eg sp1.
d2 <-data.frame(aggregate(sp1 ~ elevation + distance, data = d, sum))
Can I avoid a for loop to get a new dataframe in the same format that includes all columns sp, sp2, spn?  Trying to adapt various other solutions I have seen online have ended in failure because certain bits of my own brain are missing. Thanks.
 
    