title: Create new column containing previous value with same ID
body: <p>My data looks like this and is already sorted:</p>
  
  <pre><code>&gt; dput(df)
  structure(list(id = c(1, 2, 3, 3, 2, 2, 1), x = c(20, 40, 70, 
  70, 80, 40, 70)), .Names = c("id", "x"), row.names = c(NA, -7L
  ), class = "data.frame")
  
  &gt; df
    id  x
  1  1 20
  2  2 40
  3  3 70
  4  3 70
  5  2 80
  6  2 40
  7  1 70
  </code></pre>
  
  <p>I need to create a new column containing the previous value of <code>x</code> with same <code>id</code>. So the result should be:</p>
  
  <pre><code>&gt; df
    id  x old_x
  1  1 20    70
  2  2 40    80
  3  3 70    70
  4  3 70    NA
  5  2 80    40
  6  2 40    NA
  7  1 70    NA
  </code></pre>
  
  <p>I can do it this way:</p>
  
  <pre><code>for (i in 1:nrow(df)){
    id0 = df$id[i]
    j = i + match(id0 , df$id[i+1:nrow(df)])
    df$old_x[i] = df$x[j]
  }
  </code></pre>
  
  <p>but it's far too slow. What would be the best way to do it?</p>
  
  <p>Thanks for your help!</p>