I have a dataframe like
ID_CASE Month
CS00000026A 201301
CS00000026A 201302
CS00000026A 201303
CS00000026A 201304
CS00000026A 201305
CS00000026A 201306
CS00000026A 201307
CS00000026A 201308
CS00000026A 201309
CS00000026A 201310
CS00000191C 201302
CS00000191C 201303
CS00000191C 201304
CS00000191C 201305
CS00000191C 201306
CS00000191C 201307
CS00000191C 201308
CS00000191C 201309
CS00000191C 201310
I want the final data frame to have three additional column like
ID_CASE Month Lag_1 Lag_2 Lag_3
CS00000026A 201301 NA NA NA
CS00000026A 201302 201301 NA NA
CS00000026A 201303 201202 201201 NA
CS00000026A 201304 201203 201202 201201
CS00000026A 201305 201204 201203 201202
CS00000026A 201306 201305 201304 201303
CS00000026A 201307 201306 201305 201304
CS00000026A 201308 201307 201306 201305
CS00000026A 201309 201308 201307 201306
CS00000026A 201310 201309 201308 201307
CS00000191C 201302 NA NA NA
CS00000191C 201303 201302 NA NA
CS00000191C 201304 201303 201302 NA
CS00000191C 201305 201304 201303 201302
CS00000191C 201306 201305 201304 201303
CS00000191C 201307 201306 201305 201304
CS00000191C 201308 201307 201306 201305
CS00000191C 201309 201308 201307 201306
CS00000191C 201310 201309 201308 201307
where
I have used the following code to atleast get Lag_1
df <- ddply(df,.(ID_CASE),transform,
Lag_1 <- c(NA,Month[-nrow(df)]))
But this does not give me the desired output for Lag_1.
I have also tried looking at the solutions in Lag in R dataframe
And how can this be done if I have a date object instead of an int column 'Month' as in the current example?
Any help on this will be appreciated.
From data.table
v1.9.6
you can use shift()
:
require(data.table)
setDT(df)[, paste("lag", 1:3, sep="_") := shift(Month, 1:3), by=ID_CASE]