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
Seeliger.Curt at epamail.epa.gov
2010-Dec-09 18:03 UTC
[R] how to use diff() with different variables?
Luca wrote on 12/09/2010 09:38:07 AM:> ... > 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?A few ways. If you search for lag in the last couple of weeks, you'll see a discussion on this. One is as follows: foo <- data.frame(x=LETTERS, fine=as.Date(0:25, origin='2010-01-01')) foo$fine1<-as.Date(NA) foo[2:nrow(foo),]$fine1 <- foo[1:(nrow(foo)-1),]$fine cur -- Curt Seeliger, Data Ranger Raytheon Information Services - Contractor to ORD seeliger.curt@epa.gov 541/754-4638 [[alternative HTML version deleted]]
Maybe Matching Threads
- POSIXct value display incorrect for some values
- error in calling source(): invalid multibyte character in parser
- tabulating 2 factors weighting by a third var
- Getting total bar's label & value labels in a barplot
- Interaction between aggregate() and length()