title: Subtract data in 2 different rows
body: <p>I have data collected at time intervals from a database. The metrics are counters, as in ever-increasing. To get the value of metric for a given time, you have to subtract one row from the previous version of the same row. </p>
  
  <p>Example:</p>
  
  <pre><code>                 TS INST_ID         EVENT WAIT_TIME_MILLI WAIT_COUNT
  2014-01-29 17:20:36       1 log file sync               1     756873
  2014-01-29 17:20:36       1 log file sync               2      15627
  2014-01-29 17:20:36       1 log file sync               4       2925
  2014-01-29 17:21:03       1 log file sync               1     761063
  2014-01-29 17:21:03       1 log file sync               2      15659
  2014-01-29 17:21:03       1 log file sync               4       2929
  </code></pre>
  
  <p>Desired Output:</p>
  
  <pre><code>                 TS INST_ID         EVENT WAIT_TIME_MILLI WAIT_COUNT
  2014-01-29 17:21:03       1 log file sync               1       4190
  2014-01-29 17:21:03       1 log file sync               2         32
  2014-01-29 17:21:03       1 log file sync               4          4
  </code></pre>
  
  <p>TS is the time when the metric was collected. INST_ID, EVENT, and WAIT_TIME_MILLI are static identifiers. I want to calculate the delta of the WAIT_COUNT from one TS to the next.</p>
  
  <p>I've simplified the data a bit, but if it's important there are many events and can be multiple INST_IDs. </p>
  
  <p>Here's the test data frame:</p>
  
  <pre><code>structure(list(TS = structure(c(1391034063.541, 1391034063.541, 
  1391034063.541, 1391034036.136, 1391034036.136, 1391034036.136
  ), class = c("POSIXct", "POSIXt")), INST_ID = c(1, 1, 1, 1, 1, 
  1), EVENT = c("log file sync", "log file sync", "log file sync", 
  "log file sync", "log file sync", "log file sync"), WAIT_TIME_MILLI = c(1, 
  2, 4, 1, 2, 4), WAIT_COUNT = c(761063, 15659, 2929, 756873, 15627, 
  2925)), .Names = c("TS", "INST_ID", "EVENT", "WAIT_TIME_MILLI", 
  "WAIT_COUNT"), class = "data.frame", row.names = c(NA, 6L))
  </code></pre>