title: count zeros before a different value by ID in r
body: <p>I need to count zeros by ID in R, but I need them just the zeros before a different Value.
  Here's a example of my data.</p>
  
  <pre><code>x&lt;-tibble::tribble(
    ~ID,        ~Date, ~Value,
     1L, "01-01-2016",     0L,
     1L, "01-02-2016",     0L,
     1L, "01-03-2016",     0L,
     1L, "01-04-2016",     0L,
     1L, "01-05-2016",  1000L,
     1L, "01-06-2016",     0L,
     2L, "01-01-2016",     0L,
     2L, "01-02-2016",     0L,
     2L, "01-03-2016",  1500L,
     2L, "01-04-2016",     0L,
     2L, "01-05-2016",     0L,
     2L, "01-06-2016",     0L,
     3L, "01-01-2016",     0L,
     3L, "01-02-2016",  -300L,
     3L, "01-03-2016",     0L,
     3L, "01-04-2016",     0L,
     3L, "01-05-2016",   200L,
     3L, "01-06-2016",     0L
    )
  x&lt;-as.data.frame(x)
  
   head(x)
     #   ID       Date Value
     # 1  1 01-01-2016     0
     # 2  1 01-02-2016     0
     # 3  1 01-03-2016     0
     # 4  1 01-04-2016     0
     # 5  1 01-05-2016  1000
     # 6  1 01-06-2016     0
  </code></pre>
  
  <p>What I'm looking it's something like this</p>
  
  <pre><code>ID    1 2 3 
  Count 4 2 1
  </code></pre>
  
  <p>Because there's 4 zeros before a different value in ID 1, 2 zeros in ID 2 and 1 zero in ID 3. Also, I'd like to save it in the dataframe as it follows: </p>
  
  <pre><code>new_x&lt;-tibble::tribble(
    ~ID,        ~Date, ~Value, ~Count,
     1L, "01-01-2016",     0L,     4L,
     1L, "01-02-2016",     0L,     4L,
     1L, "01-03-2016",     0L,     4L,
     1L, "01-04-2016",     0L,     4L,
     1L, "01-05-2016",  1000L,     4L,
     1L, "01-06-2016",     0L,     4L,
     2L, "01-01-2016",     0L,     2L,
     2L, "01-02-2016",     0L,     2L,
     2L, "01-03-2016",  1500L,     2L,
     2L, "01-04-2016",     0L,     2L,
     2L, "01-05-2016",     0L,     2L,
     2L, "01-06-2016",     0L,     2L,
     3L, "01-01-2016",     0L,     1L,
     3L, "01-02-2016",  -300L,     1L,
     3L, "01-03-2016",     0L,     1L,
     3L, "01-04-2016",     0L,     1L,
     3L, "01-05-2016",   200L,     1L,
     3L, "01-06-2016",     0L,     1L
    )
  </code></pre>
  
  <p>Does anyone how to solve it? Thanks! </p>