Dear Friends,

I have some data with three columns named ID, Year and Measure X. I need to create a column which gives me a lag for each ID (note not a continous lag), but a lag conditional on the id and the given year. Please find below a sample of the data

Input file sample

ID     Year     X

AB12   2000    100
AB12   2001    120
AB12   2002    140
AB12   2003    80
BL14   2000    180
BL14   2001    150
CR93   2000    45
CR93   2001    49
CR93   2002    56
CR93   2003    67

Expected output from this data

ID     Year   Xlag
AB12   2000     .
AB12   2001   20
AB12   2002   20
AB12   2003   -60
BL12   2000    .
BL14   2001   -30
CR93   2000     .
CR93   2001     5
CR93   2002     7
CR93   2003     9

Can somebody please help me with how to implement this in R. Thanks.

Sincerely

Anup


       
---------------------------------
Looking for a deal? Find great prices on flights and hotels with Yahoo! FareChase.
	[[alternative HTML version deleted]]

