title: aggregate over rows for many columns
body: <p>I have a large dataframe in the same format as <code>d</code> below, with count data for three columns nested within two different factors.</p>
  
  <pre><code>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
  </code></pre>
  
  <p>Notice that there are two replicate rows for each level of factor <code>d$distance</code>.</p>
  
  <p>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:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>I can do it easily for one column, eg <code>sp1</code>.<br>
  <code>d2 &lt;-data.frame(aggregate(sp1 ~ elevation + distance, data = d, sum))</code></p>
  
  <p>Can I avoid a <code>for</code> loop to get a new dataframe in the same format that includes all columns <code>sp</code>, <code>sp2</code>, <code>spn</code>?  Trying to adapt various other solutions I have seen online have ended in failure because certain bits of my own brain are missing. Thanks.</p>