title: Rolling lagged differences
body: <p>Ok so I am looking to create rolling lagged differences in R. </p>
  
  <pre><code>vec &lt;- c(43.79979, 44.04865, 44.17308, 44.54638, 44.79524, 44.79524, 44.79524, 44.42195, 44.54638, 44.79524, 44.42195, 43.30206, 43.30206, 43.17764, 43.30206)
  
  &gt; length(vec)
  [1] 15
  </code></pre>
  
  <p>This is what I have tried so far: </p>
  
  <pre><code>vec1 &lt;- rollapply(vec, width = 2,  fill = NA, FUN = diff)
  </code></pre>
  
  <p>This gives this output: </p>
  
  <pre><code>[1]  0.24886  0.12443  0.37330  0.24886  0.00000  0.00000 -0.37329  0.12443  0.24886 -0.37329 -1.11989  0.00000 -0.12442  0.12442       NA
  
  &gt; length(vec1)
  [1] 15
  </code></pre>
  
  <p>Note we have an NA value in element 15. </p>
  
  <p>So I want to do this diff in lags for say lags 1,2 and 3... So the above code doesn't cater for this, so I try the below: </p>
  
  <pre><code>lag1 &lt;- diff(vec, lag = 1, differences = 1, arithmetic = TRUE, na.pad = TRUE)
  lag2 &lt;- diff(vec, lag = 2, differences = 1, arithmetic = TRUE, na.pad = TRUE)
  lag3 &lt;- diff(vec, lag = 3, differences = 1, arithmetic = TRUE, na.pad = TRUE)
  length(lag1)
  length(lag2)
  length(lag3)
  </code></pre>
  
  <p>The result of this: </p>
  
  <pre><code>&gt; lag1
   [1]  0.24886  0.12443  0.37330  0.24886  0.00000  0.00000 -0.37329  0.12443  0.24886 -0.37329 -1.11989  0.00000 -0.12442  0.12442
  &gt; lag2
   [1]  0.37329  0.49773  0.62216  0.24886  0.00000 -0.37329 -0.24886  0.37329 -0.12443 -1.49318 -1.11989 -0.12442  0.00000
  &gt; lag3
   [1]  0.74659  0.74659  0.62216  0.24886 -0.37329 -0.24886  0.00000  0.00000 -1.24432 -1.49318 -1.24431  0.00000
  &gt; length(lag1)
  [1] 14
  &gt; length(lag2)
  [1] 13
  &gt; length(lag3)
  [1] 12
  </code></pre>
  
  <p>Notice that when do the lagged difference above... it places the diff result on the line that it subtracted the value on... so it took our current value - lagged value. It places the diff result on the lagged value position. We then lose the length of the vector. I want to actually place the diff - lagged result on the start number (diff) and place leading NA's to account for the missing values at the start of the data set. </p>
  
  <p>Using lag 2 as en example, this is my desired result: </p>
  
  <pre><code>&gt; lag2
   [1]  NA    NA 0.37329  0.49773  0.62216  0.24886  0.00000 -0.37329 -0.24886  0.37329 -0.12443 -1.49318 -1.11989 -0.12442  0.00000
  </code></pre>
  
  <p>Does anyone know a way on how to correct this??</p>
  
  <p>To maybe explain a little more: </p>
  
  <p>this is is the start of the vector: </p>
  
  <pre><code>vec &lt;- c(43.79979, 44.04865, 44.17308..... 
  </code></pre>
  
  <p>So if we do a lagged 2 difference... </p>
  
  <p>We take the 3rd element... <code>44.17308</code> - <code>43.79979</code> = the result of <code>0.37329</code>. </p>
  
  <p>So I want to have NA NA <code>0.37329</code></p>
  
  <p>Instead of placing <code>0.37329</code> on the first position in the new lag2 vector. </p>