title: Is there a way to assign to a variable it value from the previous period specific to the same observation in R?
body: <p>I have a panel data like this :</p>
  <pre><code>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     
  
  </code></pre>
  <p>I want,</p>
  <pre><code>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
  
  </code></pre>
  <p>But I have 304429 different ID's, and the Panel is not ordered by year and id.</p>
  <p>Can someone please help me?</p>
  <p>Thank you!</p>