title: Calculate speed (first derivative in time) of time series
body: <p>I would like to compute the speed from data in <code>xts</code> time series. My data looks like this (command bellow generates sample, actual data is much larger).</p>
  
  <pre><code>measurement &lt;- xts(c(7.9, 8.6, 12.7, 13.3), 
                     as.POSIXct(c("2016-02-24 06:00:00",
                                  "2016-02-24 07:30:00",
                                  "2016-02-24 09:15:00",
                                  "2016-02-24 11:15:00")))
  names(measurement) &lt;- "pos"
  
  head(measurement)
                       pos
  2016-02-24 06:00:00  7.9
  2016-02-24 07:30:00  8.6
  2016-02-24 09:15:00 12.7
  2016-02-24 11:15:00 13.3
  </code></pre>
  
  <p>If I use <code>diff</code> I am getting the changes between successive values. </p>
  
  <pre><code>diff(measurement)
                      pos
  2016-02-24 06:00:00  NA
  2016-02-24 07:30:00 0.7
  2016-02-24 09:15:00 4.1
  2016-02-24 11:15:00 0.6
  </code></pre>
  
  <p>However, I would like to take time into account and compute speed (e.g. change in time period). I have tried using the following dirty road.</p>
  
  <pre><code>measurement$time  &lt;- as.numeric(index(measurement))
  measurement$speed  &lt;- diff(measurement$pos) / diff(measurement$time) * 3600
  
  head(measurement)
                      pos  time       speed
  2016-02-24 06:00:00  7.9 1456290000        NA
  2016-02-24 07:30:00  8.6 1456295400 0.4666667
  2016-02-24 09:15:00 12.7 1456301700 2.3428571
  2016-02-24 11:15:00 13.3 1456308900 0.3000000
  </code></pre>
  
  <p>There must be a simpler and more elegant way to do this. Have in mind that I am still new to R and might be missing something.</p>