Cumulative values of a column for each group (R) : <p>I have a data frame that looks like this :</p>

<pre><code>&gt; year&lt;-c(2014,2014,2014,2015,2015,2015,2016,2016,2016)
&gt; group&lt;-c("A","B","C","A","B","C","A","B","C")
&gt; n&lt;-c(1,1,1,1,2,0,2,1,1)
&gt; df&lt;-data.frame(year=year,group=group,n=n)
&gt; df

year group n
2014     A 1
2014     B 1
2014     C 1
2015     A 1
2015     B 2
2015     C 0
2016     A 2
2016     B 1
2016     C 1
</code></pre>

<p>I want to create a column that contains the cumulated values of n for each group to have something like this : </p>

<pre><code>year group n  sum
2014     A 1  1
2014     B 1  1
2014     C 1  1
2015     A 1  2
2015     B 2  3
2015     C 0  1
2016     A 2  4
2016     B 1  4
2016     C 1  2
</code></pre>
