0

I have a panel data like this :

year      ID        S(t)    

2008      N987      S1234     
2009      N987      S8875     
2010      N987      S7465     

2008      E867      F8402     
2009      E867      S1234     
2010      E867      R9879     
2011      E867      Q8687     
2012      E867      F8687     
2013      E867      J8687     

I want,

year      ID        S(t)    S(t-1)

2008      N987      S1234     0
2009      N987      S8875     S1234
2010      N987      S7465     S8875

2008      E867      F8402     0
2009      E867      S1234     F8402
2010      E867      R9879     S1234
2011      E867      Q8687     R9879
2012      E867      F8687     Q8687
2013      E867      J8687     F8687

But I have 304429 different ID's, and the Panel is not ordered by year and id.

Can someone please help me?

Thank you!

M.KKK
  • 25
  • 5
  • Using `dplyr` : `df %>% arrange(ID, year) %>% group_by(ID) %>% mutate(lag_st = lag(st, default = 0))` – Ronak Shah Nov 05 '20 at 14:48
  • @RonakShah When using it I have a message error: error in mutate_impl(.data,dots): Expecting a single string value:[type=double; extent=1]. – M.KKK Nov 05 '20 at 15:00
  • Add first few rows of your data using `dput` i.e if your dataframe is called `df` do `dput(head(df))` and edit your post. In `lag` make sure that you are using the column name present in your data. – Ronak Shah Nov 05 '20 at 15:02
  • @RonakShah now I have another message error: lag_st must be length 10 (the groupe size) or one, not 304429 (which is the numer of ids) – M.KKK Nov 05 '20 at 15:13

0 Answers0