0

I want to create lead and lag values by group and conditional on another columns

I want to create lead and lag variables for column 'Value', the 'Value_lead1' only exists when year difference is 1, otherwise it equals NaN. Also, the 'Value_lag1' only exists when year difference is 1, otherwise it equals NaN.

Group Year Value Value_lead1 Value_lag1
A 1993 1 2 NaN
A 1994 2 NaN 1
A 1996 3 4 NaN
A 1997 4 NaN 3
B 1992 1 NaN NaN
B 1997 2 3 NaN
B 1998 3 4 2
B 1999 4 NaN 3
leilei
  • 37
  • 5

0 Answers0