I have the following un-balance dataset:
id Year A
1 1 5
1 2 6
2 1 11
2 2 12
2 3 13
3 2 1
3 3 3
I would like to great a variable lagA that truly takes into account the year and id of each observation and not justs shifts the column down:
id Year A lagA
1 1 5 NA
1 2 6 5
2 1 11 NA
2 2 12 11
2 3 13 12
3 2 1 NA
3 3 3 1
Any ideas? I tried making sure that the dataframe is pf class pdata.frame
but when I use the function lag(A,1) it merely shifts the column down which produces inconsistent results.