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 : amat...@mango-solutions.com<mailto:amat...@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}} ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.