title: How to create a lag variable within each group?
body: <p>I have a data.table:</p>
  <pre><code>require(data.table)
  
  set.seed(1)
  data &lt;- data.table(time = c(1:3, 1:4),
                     groups = c(rep(c(&quot;b&quot;, &quot;a&quot;), c(3, 4))),
                     value = rnorm(7))
  
  data
  #    groups time      value
  # 1:      b    1 -0.6264538
  # 2:      b    2  0.1836433
  # 3:      b    3 -0.8356286
  # 4:      a    1  1.5952808
  # 5:      a    2  0.3295078
  # 6:      a    3 -0.8204684
  # 7:      a    4  0.4874291
  </code></pre>
  <p>I want to compute a lagged version of the &quot;value&quot; column, <em>within</em> each level of &quot;groups&quot;.</p>
  <p>The result should look like</p>
  <pre><code>#   groups time      value  lag.value
  # 1      a    1  1.5952808         NA
  # 2      a    2  0.3295078  1.5952808
  # 3      a    3 -0.8204684  0.3295078
  # 4      a    4  0.4874291 -0.8204684
  # 5      b    1 -0.6264538         NA
  # 6      b    2  0.1836433 -0.6264538
  # 7      b    3 -0.8356286  0.1836433
  </code></pre>
  <p>I have tried to use <code>lag</code> directly:</p>
  <pre><code>data$lag.value &lt;- lag(data$value) 
  </code></pre>
  <p>...which clearly wouldn't work.</p>
  <p>I have also tried:</p>
  <pre><code>unlist(tapply(data$value, data$groups, lag))
   a1         a2         a3         a4         b1         b2         b3 
   NA -0.1162932  0.4420753  2.1505440         NA  0.5894583 -0.2890288 
  </code></pre>
  <p>Which is almost what I want. However the vector generated is ordered differently from the ordering in the data.table which is problematic.</p>
  <p>What is the most efficient way to do this in base R, plyr, dplyr, and data.table?</p>