title: Summation of variables by Groups in R
body: <p>I have a data frame, and I'd like to create a new column that gives the sum of a numeric variable grouped by factors.  So something like this:</p>
  
  <p>BEFORE:</p>
  
  <pre><code>data1 &lt;- data.frame(month = c(1, 1, 2, 2, 3, 3), 
                      sex = c("m", "f", "m", "f", "m", "f"), 
                      value = c(10, 20, 30, 40, 50, 60))
  </code></pre>
  
  <p>AFTER:</p>
  
  <pre><code>data2 &lt;- data.frame(month = c(1, 1, 2, 2, 3, 3), 
                      sex = c("m", "f", "m", "f", "m", "f"), 
                      value = c(10, 20, 30, 40, 50, 60), 
                      sum = c(30, 30, 70, 70, 110, 110))
  </code></pre>
  
  <p>In Stata you can do this with the <code>egen</code> command quite easily.  I've tried the aggregate function, and the ddply function but they create entirely new data frames, and I just want to add a column to the existing one.  </p>