title: Efficiently fill NAs by group
body: <p>I have a dataset where I observe a variable for some individuals and not for others. For those individuals where I observe the variable, I observe it exactly once. However, the number of observations per individual as well as the position of the observed value varies. </p>
  
  <p>I would like to fill all NA values for a given individual with the non-NA value in case there is a non-NA value. Otherwise, NAs are supposed to remain NA.</p>
  
  <p>Here is an example dataset:</p>
  
  <pre><code>#data.frame of 100 individuals with 10 observations each
  data &lt;- data.frame(group = rep(1:100,each=10),value = NA)
  
  #first 50 individuals get a value at the fifth observation, others don't have value
  data$value[seq(5,500,10)] &lt;- rnorm(50)
  </code></pre>
  
  <p>So far so good, not a big issue. Taken from another thread, we could do something like this using <code>dplyr</code> and <code>tidyr</code>:</p>
  
  <pre><code>data &lt;- data %&gt;% 
    group_by(group) %&gt;% #by group
    fill(value) %&gt;% #default direction down
    fill(value, .direction = "up") #also fill NAs upwards
  </code></pre>
  
  <p>This solves the problem perfectly fine. However, I have to do this for around 80mio. observations, which takes hours. Is there a faster method available? I think <code>data.table</code> might be a good candidate.</p>
  
  <p>It would also be great if it would be possible to adapt the approach to fill only NAs that appear before the value.</p>
  
  <p>Thanks!</p>