title: R how to create counter and make it print to new column before every reset
body: <p>Suppose I have the following dataframe</p>
  
  <pre><code>  df
  R1 R2
  0  0 
  1  1
  1  1
  0  1
  1  1 
  0  0
  0  1
  1  0           
  0  0
  1  0
  1  0
  1  1        
  </code></pre>
  
  <p>And I wish to create a counter that counts - in every column individually - the occurrences of '1's after another, reset after every encounter of a 0, and output the counts in a new column. i.e. in row 1 it would reset at first step, then count to 1, then count to 2, then reset, then count 1, then reset, reset, etc. with the desired output for column 1 being:</p>
  
  <pre><code>  df
  R1(Counted) 
  N/A   
  N/A  
  2  
  N/A  
  1   
  N/A  
  N/A  
  1             
  N/A  
  N/A  
  N/A  
  3          
  </code></pre>
  
  <p>I suspect I need something like: </p>
  
  <pre><code>Counter = 0  
  for i = 1:nrow(df){
    if (???==1){
      counter=counter+1
    } else {
      counter=0
    }
  }  
  </code></pre>
  
  <p>But I really have no experience with counters and don't know how to make it continuously print its count to a new column before reseting the counter or anything like that.</p>
  
  <p>Any help is much appreciated </p>