title: Roll apply to return moving average
body: <p>I have a Data vector below and I'd like to calculate a 2 period moving average like this given that there are NAs in the data. So the results I am looking for are:</p>
  
  <pre><code>Data    2 period moving average
      NA
      1   1
      2   1.5
      3   2.5
      4   3.5
      5   4.5
      6   5.5
      7   6.5
      8   7.5
      9   8.5
      10  9.5
      NA   10
  </code></pre>
  
  <p>in r I as looking to use roll apply but I am not getting the right results:</p>
  
  <pre><code>z &lt;- c(NA,seq(1,10,1),NA)
  z
  rollapply(z, 2, mean, na.rm = TRUE, fill = list(NA, NULL, NA))
  [1]  1.0  1.5  2.5  3.5  4.5  5.5  6.5  7.5  8.5  9.5 10.0   NA
  </code></pre>
  
  <p>There should only be 1 NA in the result and it should be the first element because you cannot do a two period average with 1 element but the results above show the NA as the last element</p>
  
  <p>My results should be:</p>
  
  <pre><code>NA
  1
  1.5
  2.5
  3.5
  4.5
  5.5
  6.5
  7.5
  8.5
  9.5
  10
  </code></pre>