title: How to lag data table value by more than one
body: <p>Say I have a data table.  I can create a column of lagged values:</p>
  
  <pre><code>&gt;x
  date    id   val  valPr
  1/4/14   a    1     2
  1/3/14   a    2     3 
  1/2/14   a    3     4
  1/1/14   a    4     NA
  1/4/14   b    10    20
  1/3/14   b    20    30
  1/2/14   b    30    40
  1/1/14   b    40    NA
  </code></pre>
  
  <p>Using:</p>
  
  <pre><code>setDT(x)[, valPr := c(val[-1], NA), by = "id"]
  </code></pre>
  
  <p>Is there a way to do something similar to lag by more than one period?  Three for example?</p>
  
  <p>It would produce something like this:</p>
  
  <pre><code>&gt;x
  date    id   val  valPr
  1/4/14   a    1     4
  1/3/14   a    2     NA 
  1/2/14   a    3     NA
  1/1/14   a    4     NA
  1/4/14   b    10    40
  1/3/14   b    20    NA
  1/2/14   b    30    NA
  1/1/14   b    40    NA
  </code></pre>