title: R Cumulative sum based on two variables
body: <p>This is whay my data.table looks like. The last column NewShares is my desired columns.   </p>
  
  <pre><code>library(data.table)
  dt &lt;- fread('
     Client   Level   NumShares   Interest  NewShares 
      A          0     10          0         10
      A          0     0           0         10
      A          1     0          .1         11
      A          0     9           0         20
      A          1     0          .2         24')
  </code></pre>
  
  <p>I want the cumulative NumShares while taking into account NewShares bought with interest. So as of the 1st row, the cumulative shares are 10. As of the 3rd row, <code>Level==1</code>, so I have to add interest. It will be <code>10+(10*.1)=11</code>. As of the 4th row, the cumulative shares is <code>11+9 =20</code>. As of the last row, <code>Level==1</code>, so new shares are <code>20+(20*.2) = 24</code></p>
  
  <p>I tried:</p>
  
  <pre><code>dt[,NewShares:= NumShares* cumprod(1+ NumShares*Interest),by=Client]
  </code></pre>