# NOT RUN {
# The values are simply shifted
# Ahead in time
lagdf(1:10, 3)
# Back in time
lagdf(1:10, -3)
# Works but returns a numeric column
lagdf(as.factor(1:10), 3)
# Works and returns a character column
lagdf(as.character(1:10), 3)
# Giving several lag values
lagdf(1:10, c(1:3))
lagdf(1:10, c(5,3,-1))
# See also how to lag a forecast data.frame with: ?lagdf.data.frame
# dataframe of forecasts
X <- data.frame(k1=1:10, k2=1:10, k3=1:10)
X
# Lag all columns
lagdf(X, 1)
# }
# NOT RUN {
# Lag each column different steps
lagdf(X, 1:3)
# Lag each columns with its k value from the column name
lagdf(X, "+k")
# }
# NOT RUN {
# Also works for columns named hxx
names(X) <- gsub("k", "h", names(X))
lagdf(X, "-h")
# If lagseq must have length as columns in X, it doesn't know how to lag and an error is thrown
try(lagdf(X, 1:2))
# }
# NOT RUN {
# }
Run the code above in your browser using DataLab