[R] how to use diff() with different variables?

Luca Meyer lucam1968 at gmail.com
Thu Dec 9 18:38:07 CET 2010


Hi,

I first should say I am new to R. I have searched without success the R-archives to see if I could find an answer to what I am about to ask you.

My dataset is like:

  x                fine
1 A 2010-12-09 07:57:33
2 B 2010-12-09 08:05:00
3 C 2010-12-08 20:42:00
...

that is:

'data.frame':	3 obs. of  2 variables:
 $ x   : Factor w/ 3 levels "A","B","C": 1 2 3
 $ fine: POSIXct, format: "2010-12-09 07:57:33" "2010-12-09 08:05:00" "2010-12-08 20:42:00"

What I am trying to do is to build another variable fine1 that should contain the lagged value for "fine", that is:

  x                fine               fine1
1 A 2010-12-09 07:57:33 NA
2 B 2010-12-09 08:05:00 2010-12-09 07:57:33
3 C 2010-12-08 20:42:00 2010-12-09 08:05:00

How can I do that? 

Thanks,
Luca

Luca Meyer
www.lucameyer.com
IBM SPSS Statistics release 19.0.0
R version 2.12.0 (2010-10-15)
Mac OS X 10.6.5 (10H574) - kernel Darwin 10.5.0


More information about the R-help mailing list