title: R ddply sum value from next row
body: <p>I want to sum the column value from a row with the next one.</p>
  
  <pre><code>&gt; df
  
  +----+------+--------+------+
  | id |  Val | Factor | Col  |
  +----+------+--------+------+
  |  1 |   15 |      1 |    7 |
  |  3 |   20 |      1 |    4 |
  |  2 |   35 |      2 |    8 | 
  |  7 |   35 |      1 |   12 |
  |  5 |   40 |      1 |   11 |
  |  6 |   45 |      2 |   13 |
  |  4 |   55 |      1 |    4 |
  |  8 |   60 |      1 |    7 |
  |  9 |   15 |      2 |   12 |
  ..........
  </code></pre>
  
  <p>I would like to have the mean of sum of the <code>Row$Val</code> + <code>nextRow$Val</code> based on their <code>id</code> and <code>Col</code>. I can't assume that the <code>id</code> or <code>Col</code> are consecutive. </p>
  
  <p>I am using ddply to summarize my df. I have tried</p>
  
  <pre><code>&gt; ddply(df, .(Factor), summarize, 
         max(Val), 
         sum(Val), 
         mean(Val + df[df$id == id+1 &amp; df$Col = Col]$Val)
         )
  
  &gt; "longer object length is not a multiple of shorter object length"
  </code></pre>