title: restart counting under conditions in R
body: <p>I have a flag column that contains continuous streams 1s and 0s. I want to add the stream of 1s. When it encounters 0s, the summing should stop. For the next stream of 1s, summing should start afresh</p>
  
  <p>I have tried cumsum(negread_flag == 1) this continues to sum after the 0s</p>
  
  <pre><code>negread_flag   result
  1               1
  1               2 
  1               3  
  1               4 
  0               0 
  0               0
  0               0
  1               1
  1               2
  1               3
  0               0
  </code></pre>