How can we lag the series in order to feed it to the models ?´
library(dplyr) # Sample data eg <- data.frame(dat = c("a","b","c"), Jan = c(1,2,3), Feb = c(4,5,6), Mar = c(7,8,9)) # Repeat each row eg %>% slice(rep(1:n(), each = 3)) # expected output exp_out <- data.frame(dat = c(rep("a",3),rep("b",3),rep("c",3)), somecolumnName1 = c(NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN,NaN), somecolumnName2 = c(NaN,NaN,1,NaN,NaN,2,NaN,NaN,3), somecolumnName3 = c(NaN,1,4,NaN,2,5,NaN,3,6), Jan = c(1,4,7,2,5,8,3,6,9), Feb = c(4,7,NA,5,8,NA,6,9,NA), Mar = c(7,NA,NA,8,NA,NA,9,NA,NA))
This is actually from 3 rows and 3 months but how can we do the same shifting operations for 30,000 rows and 36 months ?
Tried with dplyr::lag but it shifts column wise but not row wise
Could you please advise me