title: R: Insert and fill missing periods in panel data
body: <p>I'm trying to learn R coming from Stata, but have run into the following two problems which I cannot seem to find elegant solutions for in R:</p>
  
  <p>1) I have a panel dataset with gaps in my time variable. I would like to expand my time variable to include the gaps despite having no observed data for these rows.</p>
  
  <p>In Stata I would usually go about this by setting my ID and time variables with <code>xtset</code> and then expanding the dataset based on this with <code>tsfill</code>. Is there an equivalently elegant way in R?</p>
  
  <p>2) I would like to fill some of the new, blank cells with data for constant variables.</p>
  
  <p>In Stata I would do this by copying data from previous (relative to my time variable) observations using the <code>l.</code>-prefix; for example using <code>replace Con = l.Con</code>.</p>
  
  <p>In other words I'm asking how to go from something like this:</p>
  
  <pre><code>ID     Time     Num     Con
  1      Jan      10      A
  1      Feb      15      A
  1      May      20      A
  2      Feb      12      B
  2      Mar      14      B
  2      Jun      15      B
  </code></pre>
  
  <p>To something like this:</p>
  
  <pre><code>ID     Time     Num     Con
  1      Jan      10      A
  1      Feb      15      A
  1      Mar              A
  1      Apr              A
  1      May      20      A
  2      Feb      12      B
  2      Mar      14      B
  2      Apr              B
  2      May              B
  2      Jun      15      B
  </code></pre>
  
  <p>Hopefully that makes sense. Thanks in advance.</p>