Hi R-helpers, I have a dataframe as shown below
ID YEAR_MONTH ATT_1 ATT_2 1 201301 Y 1 1 201302 Y 1 1 201302 N 0 1 201302 Y 0 1 201303 N 1 3 201301 N 1 3 201302 N 0 3 201302 Y 0 3 201302 Y 1 3 201303 Y 1 The final dataframe I want should look like ID YEAR_MONTH YEARMONTH_LAG1 ATT1_CHNG ATT2_CHNG 1 201301 NA NA NA 1 201302 201301 0 0 1 201303 201302 2 1 3 201301 NA NA NA 3 201302 201301 0 0 3 201303 201302 1 1 Note: 1. 'YEARMONTH_LAG1 ' is the previous month corresponding to the current month. E.g., if YEAR_MONTH == 201301 then YEARMONTH_LAG1 = NA (as there is no record for 201212, and there is none in my data as all starts from 201201). Similarly if YEAR_MONTH == 201302 then YEARMONTH_LAG1 = 201301. 2. ATT1_CHNG is the number of times the level has changed (i.e., from Y to N and vice-verse) for 'ATT_1' in the previous month (i.e., in YEARMONTH_LAG1) 3. ATT2_CHNG is the number of times the level has changed (i.e., from 0 to 1 and vice-verse) for ATT_2 in the previous month How can this be done in R? -- Regards Abhinaba Roy [[alternative HTML version deleted]] ______________________________________________ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.