title: cumsum is.na with rle ignoring consectives NA's
body: <p>simple problem. Lets say I have the following data:</p>
  
  <pre><code>library(tidyverse)
  df &lt;- data.frame(group = c(1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 2, 2),
                       variable = c(NA, "a", NA, "b", "c", NA, NA, NA, NA, "a", NA, "c", NA, NA, "d", NA, NA, "a"))
  df
     group variable
  1      1     &lt;NA&gt;
  2      1        a
  3      1     &lt;NA&gt;
  4      1        b
  5      1        c
  6      1     &lt;NA&gt;
  7      1     &lt;NA&gt;
  8      1     &lt;NA&gt;
  9      1     &lt;NA&gt;
  10     1        a
  11     1     &lt;NA&gt;
  12     1        c
  13     1     &lt;NA&gt;
  14     1     &lt;NA&gt;
  15     1        d
  16     2     &lt;NA&gt;
  17     2     &lt;NA&gt;
  18     2        a
  </code></pre>
  
  <p>I just want to count missing variables using <code>cumsum(is.na(variable)</code> but ignore consecutive missing ones so my desired output would look like:</p>
  
  <pre><code>   group variable newvariable
  1      1     &lt;NA&gt;           1
  2      1        a           1
  3      1     &lt;NA&gt;           2
  4      1        b           2
  5      1        c           2
  6      1     &lt;NA&gt;           3
  7      1     &lt;NA&gt;           3
  8      1     &lt;NA&gt;           3
  9      1     &lt;NA&gt;           3
  10     1        a           3
  11     1     &lt;NA&gt;           4
  12     1        c           4
  13     1     &lt;NA&gt;           5
  14     1     &lt;NA&gt;           5
  15     1        d           5
  16     2     &lt;NA&gt;           1
  17     2     &lt;NA&gt;           1
  18     2        a           1
  </code></pre>
  
  <p>I think I need to incorporate <code>rle</code> into my code:</p>
  
  <pre><code>df %&gt;%
    group_by(group, na_group = {na_group = rle(variable); rep(seq_along(na_group$lengths), na_group$lengths)}) %&gt;%
    mutate(newvariable = cumsum((is.na(variable)))) #?
  </code></pre>
  
  <p>Maybe <code>map</code> over groups could work. Any suggestions please?</p>
  
  <p>Refs:
  <a href="https://stackoverflow.com/questions/54756104/identify-sets-of-na-in-a-vector">Identify sets of NA in a vector</a> 
  <a href="https://stackoverflow.com/questions/40083160/count-consecutive-values-in-groups-with-condition-with-dplyr-and-rle">Count consecutive values in groups with condition with dplyr and rle</a></p>