title: R data.frame add a column depending on row-values
body: <p>In R, I have a data.frame that looks like this:</p>
  
  <pre><code> X  Y
   20 7
   25 84
   15 62
   22 12
   60 24
   40 10
   60 60
   12 50
   11 17
  </code></pre>
  
  <p>now, i want a new Colum, lets call it "SumX", that adds two following values of X into a new field of that SumX column, and one that does the same to "SumY" column. So the result data.frame would look like this:</p>
  
  <pre><code>X   Y   SumX                 SumY
  20  7   20   #first row = X  7   #first row = Y
  25  84  45   #X0 + X1        91  #Y0 + Y1
  15  62  40   #X1 + X2        146 #Y1 + Y2
  22  12  37   #X2 + X3        74  #Y2 + Y3
  60  24  82   #X3 + X4        36  #Y3 + Y4
  40  10  100  #X4 + X5        34  #Y4 + Y5
  60  60  100  #and so on      70  #and so on
  12  50  72                   110
  11  17  23                   67
  </code></pre>
  
  <p>I can do simple X + Y into a new column with </p>
  
  <pre><code>myFrame$SumXY &lt;- with(myFrame, X+Y)
  </code></pre>
  
  <p>but it there a simple way to add two X (n + (n-1)) values into SumX, and two Y (n + (n-1)) into SumY? Even if it is with a while-loop, though i would prefer a simpler way (its a lot of data like this).  Any help is much appreciated! (I'm still pretty new to R)</p>