title: Create lagged columns using data.table
body: <p><strong>Background:</strong><br>
  Lets say I have this code</p>
  
  <pre><code>library(data.table)
  
  #reproducibility
  set.seed(45L)
  
  #make table
  dt &lt;- data.table(V1=c(1L,2L),
                   V2=LETTERS[1:3],
                   V3=round(rnorm(4),4),
                   V4 = 1:12)
  dt
  </code></pre>
  
  <p>for which I get</p>
  
  <pre><code>&gt; dt
      V1 V2      V3 V4
   1:  1  A  0.3408  1
   2:  2  B -0.7033  2
   3:  1  C -0.3795  3
   4:  2  A -0.7460  4
   5:  1  B  0.3408  5
   6:  2  C -0.7033  6
   7:  1  A -0.3795  7
   8:  2  B -0.7460  8
   9:  1  C  0.3408  9
  10:  2  A -0.7033 10
  11:  1  B -0.3795 11
  12:  2  C -0.7460 12
  </code></pre>
  
  <p>and I want to append create 10 columns of the lags of V3.</p>
  
  <p><strong>Question:</strong><br>
  Is there a way to do it within the "data-table" paradigm.</p>
  
  <p><strong>More details:</strong><br>
  If it were a data.frame then I could do a loop.</p>
  
  <pre><code>dt &lt;- as.data.frame(dt)
  
  
  for(i in 1:10){
       dt &lt;- cbind(dt, shift(x = dt[, 3], 
                             n = i, 
                             fill = NA, 
                             type = "lag"))
       names(dt)[ncol(dt)] &lt;- sprintf("lag_%06d",i)
  }
  
  dt
  </code></pre>
  
  <p>for which I get:</p>
  
  <pre><code>&gt; dt
     V1 V2      V3 V4 lag_000001 lag_000002 lag_000003 lag_000004 lag_000005 lag_000006 lag_000007 lag_000008 lag_000009 lag_000010
  1   1  A  0.3408  1         NA         NA         NA         NA         NA         NA         NA         NA         NA         NA
  2   2  B -0.7033  2     0.3408         NA         NA         NA         NA         NA         NA         NA         NA         NA
  3   1  C -0.3795  3    -0.7033     0.3408         NA         NA         NA         NA         NA         NA         NA         NA
  4   2  A -0.7460  4    -0.3795    -0.7033     0.3408         NA         NA         NA         NA         NA         NA         NA
  5   1  B  0.3408  5    -0.7460    -0.3795    -0.7033     0.3408         NA         NA         NA         NA         NA         NA
  6   2  C -0.7033  6     0.3408    -0.7460    -0.3795    -0.7033     0.3408         NA         NA         NA         NA         NA
  7   1  A -0.3795  7    -0.7033     0.3408    -0.7460    -0.3795    -0.7033     0.3408         NA         NA         NA         NA
  8   2  B -0.7460  8    -0.3795    -0.7033     0.3408    -0.7460    -0.3795    -0.7033     0.3408         NA         NA         NA
  9   1  C  0.3408  9    -0.7460    -0.3795    -0.7033     0.3408    -0.7460    -0.3795    -0.7033     0.3408         NA         NA
  10  2  A -0.7033 10     0.3408    -0.7460    -0.3795    -0.7033     0.3408    -0.7460    -0.3795    -0.7033     0.3408         NA
  11  1  B -0.3795 11    -0.7033     0.3408    -0.7460    -0.3795    -0.7033     0.3408    -0.7460    -0.3795    -0.7033     0.3408
  12  2  C -0.7460 12    -0.3795    -0.7033     0.3408    -0.7460    -0.3795    -0.7033     0.3408    -0.7460    -0.3795    -0.7033
  </code></pre>
  
  <p>There has to be a more elegant way.  Something that can handle much larger data much more quickly and efficiently.</p>
  
  <p>Now what if I wanted to first do this, to make 10 lags, on column V3, then on V4, without presupposing their names.  I could simply make nested for loops, but again I suspect that data.table has something good (great?) to offer there.</p>