title: Aggregating values in a data frame based on key
body: <p>I've got a piece of aggregation code that works well enough but runs a bit slow against a data frame with 10e6 rows. I'm not that experienced in R so apologies for my cringe worthy code!</p>
  
  <p>I just want to do a basic roll up and sum of values for a common key...</p>
  
  <p>eg go from...</p>
  
  <pre><code>  key val
  1   a   5
  2   b   7
  3   a   6
  </code></pre>
  
  <p>to...</p>
  
  <pre><code>  key val
  1   a   11
  2   b   7
  </code></pre>
  
  <p>the best i can manage is...</p>
  
  <pre><code>keys = unique(inp$key)
  vals = sapply(keys, function(x) { sum(inp[inp$key==x,]$val) })
  out = data.frame(key=keys, val=vals)
  </code></pre>
  
  <p>I have this gut feel that the <code>inp[inp$key==x,]</code> is not the best way. Is there an obvious speed up i'm missing? I can do it in Hadoop (since the 10e6 dataset is actually already a rollup from a 2e9 row dataset) but I'm trying to improve my R.</p>
  
  <p>Cheers,
  Mat</p>