title: Applying function to multiple rows using values from multiple rows
body: <p>I have created the following simple function in R:</p>
  
  <pre><code>fun    &lt;- function(a,b,c,d,e){b+(c-a)*((e-b)/(d-a))}
  </code></pre>
  
  <p>That I want to apply this function to a <code>data.frame</code> that looks something like:</p>
  
  <pre><code>&gt; data.frame("x1"=seq(55,75,5),"x2"=round(rnorm(5,50,10),0),"x3"=seq(30,10,-5))
    x1 x2 x3
  1 55 51 30
  2 60 45 25
  3 65 43 20
  4 70 57 15
  5 75 58 10
  </code></pre>
  
  <p>I want to apply <code>fun</code> to each separate row to create a new variable <code>x4</code>, but now comes the difficult part (to me at least..): for the arguments d and e I want to use the values <code>x2</code> and <code>x3</code> from the next row. So for the first row of the example that would mean: <code>fun(a=55,b=51,c=30,d=45,e=25)</code>. I know that I can use <code>mapply()</code> to apply a function to each row, but I have no clue on how to tell mapply that it should use some values from the next row, or whether I should be looking for a different approach than <code>mapply()</code>? </p>
  
  <p>Many thanks in advance!</p>