Hello, I want to use lag on a time variable but I have to take date into consideration ie I don't want days to overlap ie: I don't want my first time of today to match my last time of yeterday. In SAS I would use : data x; set y; by date tim; previous=lag(tim); if first.date then do; previous=.; end; run; How can I do something similar in R? I can't find any examples anywhere. Thank you all for your help. -- View this message in context: http://r.789695.n4.nabble.com/Can-you-have-a-by-variable-in-Lag-function-as-in-SAS-tp4649647.html Sent from the R help mailing list archive at Nabble.com.
Nordlund, Dan (DSHS/RDA)
2012-Nov-15 23:01 UTC
[R] Can you have a by variable in Lag function as in SAS
> -----Original Message----- > From: r-help-bounces at r-project.org [mailto:r-help-bounces at r- > project.org] On Behalf Of ramoss > Sent: Thursday, November 15, 2012 11:56 AM > To: r-help at r-project.org > Subject: [R] Can you have a by variable in Lag function as in SAS > > Hello, > > I want to use lag on a time variable but I have to take date into > consideration ie I don't want days to overlap ie: > I don't want my first time of today to match my last time of yeterday. > > In SAS I would use : > > data x; > set y; > by date tim; > previous=lag(tim); > if first.date then > do; > previous=.; > end; > run; > > How can I do something similar in R? I can't find any examples > anywhere. > > Thank you all for your help. > >I haven't seen a response to this question yet, so here is an approach that may work for you. Let's say you have a data frame called dat that contains your variables date and tim. Then using the Lag() function from the Hmisc package you could do something like this library(Hmisc) dat$previous <- ave(dat$tim,dat$date,FUN=Lag) Hope this is helpful, Dan Daniel J. Nordlund Washington State Department of Social and Health Services Planning, Performance, and Accountability Research and Data Analysis Division Olympia, WA 98504-5204
Agnieszka Matoga
2012-Nov-16 11:04 UTC
[R] Can you have a by variable in Lag function as in SAS
Hi Ramoss, There are a few solutions to this - probably the best solution involves proper handling of time series/date objects. But, for "an" answer try: # Build a data frame ... set.seed(123) someTimes <- sapply(1:5, function(i) sort(round(runif(6, 0, 24)))) myDf <- data.frame(Day = rep(1:5, each = 6), Hour = as.vector(someTimes)) # Do the lagging ... do.call("rbind", lapply(split(myDf, myDf$Day), function(df) transform(df, Lagged = c(NA, df$Hour[-nrow(df)])))) Or you could use the "Lag" function (not the "lag" function) from Hmisc: library(Hmisc) set.seed(123) someTimes <- sapply(1:5, function(i) sort(round(runif(6, 0, 24)))) myDf <- data.frame(Day = rep(1:5, each = 6), Hour = as.vector(someTimes)) # Do the lagging ... do.call("rbind", lapply(split(myDf, myDf$Day), function(df) transform(df, Lagged = Lag(df$Hour)))) kind regards, --------------- Aga Matoga : amatoga@mango-solutions.com<mailto:amatoga@mango-solutions.com> http://www.mango-solutions.com<http://www.mango-solutions.com/>, +44 (0)1249 705 450 -- LEGAL NOTICE\ \ This message is intended for the use of ...{{dropped:22}}
Thank you again all responders. Dan your solution was both easy & miraculous. -- View this message in context: http://r.789695.n4.nabble.com/Can-you-have-a-by-variable-in-Lag-function-as-in-SAS-tp4649647p4649773.html Sent from the R help mailing list archive at Nabble.com.