title: Ordering followed by cumulative sum + by
body: <p>Here is my data :</p>
  
  <pre><code>class x1  x2
  c     6   90
  b     5   50
  c     3   70
  b     9   40
  a     5   30
  b     1   60
  a     7   20
  c     4   80
  a     2   10
  </code></pre>
  
  <p>I first want to order it by <code>class</code> (increasing or decreasing doesn't really matter) and then by <code>x1</code> (decreasing), so I do the following :</p>
  
  <pre><code>df &lt;- df[with(df, order(class, x1, decreasing = TRUE))]
  
  class x1  x2
  c     6   90
  c     4   80
  c     3   70
  b     9   40
  b     5   50
  b     1   60
  a     7   20
  a     5   30
  a     2   10
  </code></pre>
  
  <p>And then I would like the cumulative sum over <code>x1</code> for each <code>class</code> :</p>
  
  <pre><code>class x1  x2  cumsum  
  c     6   90  90      
  c     4   80  170     # 90+80
  c     3   70  240     # 90+80+70
  b     9   40  40      
  b     5   50  90      # 40+50
  b     1   60  150     # 40+50+60
  a     7   20  20
  a     5   30  50      # 20+30
  a     2   10  60      # 20+30+10
  </code></pre>
  
  <p>Following <a href="https://stackoverflow.com/a/35340031/4348534">this answer</a>, I did this :</p>
  
  <pre><code>df$cumsum &lt;- unlist(by(df$x2, df$class, cumsum))
  # (Also tried this, same result)
  df$cumsum &lt;- unlist(by(df[,x2], df[,class], cumsum))
  </code></pre>
  
  <p>But what I get is a cumulative sum over the whole set + misordered. To be more specific, Here is what I get :</p>
  
  <pre><code>class x1  x2  cumsum  
  c     6   90  20   # this cumsum
  c     4   80  50   # and this cumsum
  c     3   70  60   # and this cumsum are the cumsum of the lines of class a,
  b     9   40  100  # then it adds the 'x2' values of class b : 60 ('cumsum' from the previous line) + 40   
  b     5   50  150  # and keeps doing so : 100 + 50
  b     1   60  210  # 150 + 60
  a     7   20  300  # 210 + 90
  a     5   30  380  # 300 + 80
  a     2   10  450  # 380 + 70
  </code></pre>
  
  <p>Any idea on how I could solve this ? Thanks</p>