title: r zoo package rollapply function
body: <p>Beloe is my test data and code to summarise the tbl table by counting num of positive values. Then add 5 consecutive row counts using <code>rollapply</code> and FUN sum. I am getting NA at rows 1,2 - 5,6,7,8 - 10,11.  </p>
  
  <p>NA at 5,6 and 10, 11 is due to the missing next rows which is expected but I don't understand why I am getting NA at rows 1,2 and 7,8. Can some take a look at the code and point my mistake?</p>
  
  <pre><code>library(tidyverse)
  library(zoo)
  
  tbl&lt;-tribble(
    ~z, ~x, ~y,
    "x", "a", 2,
    "x", "b", 1,
    "x", "b", 3,
    "y", "c", 3,
    "x", "c", 1,
    "x", "d", -1,
    "x", "q", 2,
    "x", "q", 2,
    "x", "a", 2,
    "x", "s", -1,
    "y", "q", -1,
    "y", "b", 3,
    "x", "c", 3,
    "y", "c", -1,
    "y", "q", 1,
    "y", "w", 2,
    "y", "w", -2,
    "y", "t", 2,
    "y", "t", 1
  )
  
  tbl %&gt;%
    group_by(z, x) %&gt;%
    summarise(xy = sum(y&gt;0, na.rm = T))%&gt;%
    mutate(zzz = rollapply(xy, width=5, sum, fill=NA))
  </code></pre>
  
  <p>Output:</p>
  
  <pre><code># A tibble: 11 x 4
  # Groups:   z [2]
         z     x    xy   zzz
     &lt;chr&gt; &lt;chr&gt; &lt;int&gt; &lt;dbl&gt;
   1     x     a     2    NA
   2     x     b     2    NA
   3     x     c     2     8
   4     x     d     0     6
   5     x     q     2    NA
   6     x     s     0    NA
   7     y     b     1    NA
   8     y     c     1    NA
   9     y     q     1     6
  10     y     t     2    NA
  11     y     w     1    NA
  </code></pre>