title: R programming: How can I compute the difference between two cells in a data frame and save them in a new column
body: <p>Trying to learn R and am stuck on an autocorrelation example. I want to regress the difference in x against the difference in y. I have x and y in a data frame, and would like the difference of x2 - x1 to be saved in a new column say dx. I have no idea how to go about this.</p>
  
  <p>what I have:</p>
  
  <p>data1</p>
  
  <pre><code>x   y
  5   3
  8   9
  3   1
  1   5
  .   .
  .   .
  .   .
  </code></pre>
  
  <p>what I would like to get:</p>
  
  <pre><code>data1.dif
  
  x   y   dx   dy
  5   3   NA   NA
  8   9    3    6
  3   1   -5   -8
  1   5   -2    4
  .   .    .    .
  .   .    .    .
  </code></pre>