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}}


