title: Cumulative sum of a subset of data based on condition
body: <p>I have what i think is a simple R task but i'm having trouble. Basically I need to do a cumulative sum of values based on the criteria of another column.</p>
  <p>Here's the catch, it should do the cumulative sum for the previous rows until it hits another condition. In the example i'm providing, it accumulates all values from the duration column, 1 and 2 in the condition column. Example is shown below.</p>
  <pre><code>duration &lt;- c(2,3,2,4,5,10,2,9,7,5,8,9,10,12,4,5,6)
  condition &lt;- c(0,1,0,0,0,0,0,2,0,0,0,0,1,0,0,0,2)
  accum_sum &lt;- c(0,5,0,0,0,0,0,32,0,0,0,0,39,0,0,0,27)
  
  df &lt;- data.frame(duration,condition,accum_sum)
  df
  row    duration condition accum_sum
  1         2         0         0
  2         3         1         5
  3         2         0         0
  4         4         0         0
  5         5         0         0
  6        10         0         0
  7         2         0         0
  8         9         2        32
  9         7         0         0
  10        5         0         0
  11        8         0         0
  12        9         0         0
  13       10         1        39
  14       12         0         0
  15        4         0         0
  16        5         0         0
  17        6         2        27
  </code></pre>