title: Backfilling in data in r
body: <p>I have a data set of individuals with differing numbers of repeated observations and a value that is sometimes only filled in the final observation, say:</p>
  
  <pre><code>id &lt;- c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3)
  order &lt;- c(1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4)
  value &lt;- c(NA, NA, NA, 3, NA, NA, NA, 6, NA, NA, NA, 1)
  x &lt;- data.frame(id, order, value)
  </code></pre>
  
  <p>Where ID represents each individual, order is the order of observations taken (1 is first, increasing by 1 with subsequent observations) and value is some value that needs backfilling (i.e. I need the NA's for each ID to be filled in a value if it is missing).</p>
  
  <p>I am stuck on how to do this. I tried ordering the data first:</p>
  
  <pre><code>x &lt;- x[order(x$id, -x$order, x$value),]
  </code></pre>
  
  <p>But I could not fathom how to get the code to work by selecting the previous observation that way (i.e. if id = previous id &amp; value is missing, take previous version of value). The data is large (13m records) and there are many different numbers of order there (most have 1 observation, some may have up to 10). What would be the best way to do this?</p>