R Question: calculating deltas in a timeseries

Posted by Jörg Beyer on Stack Overflow See other posts from Stack Overflow or by Jörg Beyer
Published on 2010-04-09T20:00:31Z Indexed on 2010/04/09 20:03 UTC
Read the original article Hit count: 432

Filed under:

I have a timeseries of samples in R:

> str(d)
'data.frame': 5 obs. of  3 variables:
 $ date: POSIXct, format: "2010-03-04 20:47:00" "2010-03-04 21:47:00" ...
 $ x   : num  0 10 11 15.2 20
 $ y   : num  0 5 7.5 8.4 12.5
> d
                 date    x    y
1 2010-03-04 20:47:00  0.0  0.0
2 2010-03-04 21:47:00 10.0  5.0
3 2010-03-04 22:47:00 11.0  7.5
4 2010-03-04 23:47:00 15.2  8.4

5 2010-03-05 00:47:00 20.0 12.5

In this example samples for x and y are taken every hour (but the time delta is not fix). The x and y values are always growing (like a milage counter in a car). I need the deltas, how much was the growth in between, something like this:

1 2010-03-04 20:47:00  0.0  0.0
2 2010-03-04 21:47:00 10.0  5.0
3 2010-03-04 22:47:00 1.0   2.5
4 2010-03-04 23:47:00 4.2   0.9
5 2010-03-05 00:47:00 4.8   4.1

And I also need the detlas per time (x and y delta, divided by the time - delta per hour)

How would I do this in R?

© Stack Overflow or respective owner

Related posts about r