title: Retain and lag function in R as SAS
body: <p>I am looking for a function in R similar to <code>lag1</code>, <code>lag2</code> and <code>retain</code> functions in SAS which I can use with data.tables.</p>
  
  <p>I know there are functions like <code>embed</code> and <code>lag</code> in R but they don't return a single value or the previous value . They return a complete set of vectors.</p>
  
  <p>Is there anything in R which I can use with data.table?</p>
  
  <p>More info on the SAS functions :</p>
  
  <ul>
  <li><a href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000214163.htm" rel="noreferrer">Retain</a> </li>
  <li><a href="http://support.sas.com/documentation/cdl/en/lrdict/64316/HTML/default/viewer.htm#a000212547.htm" rel="noreferrer">Lag</a></li>
  </ul>