title: How to do cumsum of 2 groups by dplyr?
body: <p>I wrote  the following code:</p>
  
  <pre><code>set.seed(20)
  dat &lt;- data.frame(item=c(rep("i1", 10), rep("i2", 10)),
                    choice=c(sample(1:4, 20, replace = TRUE)))
  </code></pre>
  
  <p>I need to get the cumulative sums of each choice by the 2 item:</p>
  
  <pre><code>  item  choice     n     cumsum  
  1 i1         1     2          2
  2 i1         2     3          5
  3 i1         3     1          6
  4 i1         4     4         10
  5 i2         1     3          3
  6 i2         2     3          6
  7 i2         3     2          8
  8 i2         4     2         10
  </code></pre>
  
  <p>I wrote:</p>
  
  <pre><code>dat %&gt;% 
    group_by(item, choice) %&gt;% 
    count() %&gt;% 
    mutate(n) %&gt;%
    mutate(cum=cumsum(n))
  </code></pre>
  
  <p>And get:</p>
  
  <pre><code>  item  choice     n   cum
    &lt;fct&gt;  &lt;int&gt; &lt;int&gt; &lt;int&gt;
  1 i1         1     2     2
  2 i1         2     3     3
  3 i1         3     1     1
  4 i1         4     4     4
  5 i2         1     3     3
  6 i2         2     3     3
  7 i2         3     2     2
  8 i2         4     2     2
  </code></pre>
  
  <p>How should I modify my code to get what I need?</p>