title: Calculate increments of a variable in R
body: <p>I have to calculate the increments of a variable <code>m</code> for a time interval (t2-t1).
  Here is a dummy version of my data frame:</p>
  
  <pre><code>df &lt;- expand.grid(m = do.breaks(c(1, 10), 5),
                    sample = c("A", "B", "C", "D"))
  df$t &lt;- rep(1:6, 4, ordered = TRUE)
  df$d_m &lt;- NA
  </code></pre>
  
  <p>what I'm trying to do is to populate <code>df$d_m[i]</code> with the difference between <code>df$m[i+1]</code> and <code>df$m[i]</code>, 
  also this has to be done within each level of sample.
  So this is my attempt, but is not successful at all.</p>
  
  <pre><code>delta_m &lt;- function(m, t){
                 for(i in 1:length(t)){
                 df$d_m[i] &lt;- m[i+1] - m[i]
                 }}
  
  df &lt;- ddply(df, .(sample, t), transform, d_m = delta_m(m, t))
  </code></pre>
  
  <p>Where am I wrong?</p>