title: Calculate rolling difference in R
body: <p>I'm trying to do a rolling difference in a dataset. I have a table like this:</p>
  
  <pre><code>Year  Count
  2017  5
  2017  6
  2017  7
  2017  6
  2017  8
  </code></pre>
  
  <p>And I would like to get a column for difference, which is calculated from the 5th row onward. So it will look like this</p>
  
  <pre><code>Index  Count  Diff
  1      5      NA
  2      6      NA
  3      7      NA
  4      6      NA
  5      8      NA
  6      3      -2
  7      4      -2
  8      9      2
  9      2      -4
  10     1      -7
  </code></pre>
  
  <p>Right now I just subset the count column and combine with zeros for the calculation. Is there a more tidy way of doing this?</p>
  
  <p>Here's what I'm doing now:</p>
  
  <pre><code>a &lt;- df$Count[1:5]
  b &lt;- rep(0,5)
  df$Count1 &lt;- c(b,a)
  df$Diff &lt;- df$Count - df$Count1
  </code></pre>