title: count consecutive occurrence and stop once found value
body: <p>I have a data frame that looks like this:</p>
  
  <pre><code>account &lt;- c('123','123','123','123')
  bin &lt;- c(3,6,9,12)
  count &lt;- c(0,0,2,0)
  
  df &lt;- data.frame(account,bin,count)
  df
  &gt; df
    account bin count
  1     123   3     0
  2     123   6     0
  3     123   9     2
  4     123  12     0
  </code></pre>
  
  <p>I want an output that looks like this:  </p>
  
  <pre><code>  &gt; df
        account bin count cumCount
      1     123   3     0    1
      2     123   6     0    2
      3     123   9     2    0
      4     123  12     0    0
  </code></pre>
  
  <p>Basically, I need to count the number of consecutive zeror starting from <code>bin = 3</code>. But once <code>count</code> columns is <code>&gt;0</code> I want the rest of the values to be zero.  </p>
  
  <p>I've looked around the web a bit and here are 2 part solutions that are almost there:  </p>
  
  <pre><code>df %&gt;% 
    group_by(count) %&gt;% 
    mutate(id = row_number())
  
  
  # A tibble: 4 x 4
  # Groups:   count [2]
    account   bin count    id
     &lt;fctr&gt; &lt;dbl&gt; &lt;dbl&gt; &lt;int&gt;
  1     123     3     0     1
  2     123     6     0     2
  3     123     9     2     1
  4     123    12     0     3
  </code></pre>
  
  <p>And</p>
  
  <pre><code>   df %&gt;% 
    mutate( x = sequence(rle(
      as.character(count))$lengths))
  
  &gt; df %&gt;% 
  +   mutate( x = sequence(rle(
  +     as.character(count))$lengths))
    account bin count x
  1     123   3     0 1
  2     123   6     0 2
  3     123   9     2 1
  4     123  12     0 1
  </code></pre>
  
  <p>but they still keep counting after zero is found.<br>
  Is there another solution?</p>