Hi R helpers,

I have a dataframe as,

  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 output as,


  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  201307CS00000191C 201301  NA       NA     NA
CS00000191C 201302  201301   NA     NA
CS00000191C 201303  201302  201301  NA
CS00000191C 201304  201303  201302  201301
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

   - Lag_1 is lagged by 1 Month
   - Lag_2 is lagged by 2 Months
   - Lag_3 is lagged by 3 Months.

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.

And how can this be done if I have a *date-time* object instead of an
*int*column 'Month' as in the current example?

Any help on this will be appreciated.




-- 
Regards
Abhinaba Roy
Statistician

	[[alternative HTML version deleted]]

