title: R: Replace missing values in one row based on unique ID for all variables in dataframe
body: <p>I'm working with a &quot;long&quot; dataframe in which unique participants have multiple observations over time. Some rows are missing data across multiple columns, and those missing records need to be filled with the same data for each participant.</p>
  <p>My data set looks like:</p>
  <pre><code>list(
  ID = c(&quot;A23&quot;, &quot;A23&quot;, &quot;A24&quot;, &quot;A24&quot;, &quot;A24&quot;, &quot;A50&quot;, &quot;A50&quot;, &quot;A60&quot;, &quot;A60&quot;), 
  A = c(&quot;Y&quot;, &quot;Y&quot;, &quot;N&quot;, &quot;N&quot;, NA, &quot;Y&quot;, NA, &quot;N&quot;, &quot;N&quot;), 
  B = c(2.4, 2.4, 2.6, 2.6, NA, 2.2, NA, 3.3, 3.3), 
  C = c(&quot;M&quot;, &quot;M&quot;, &quot;F&quot;, &quot;F&quot;, NA, &quot;F&quot;, NA, &quot;M&quot;, &quot;M&quot;), 
  D = c(1, 1, 0, 0, NA, 1, NA, 1, 1), 
  E = c(&quot;2015-02-16&quot;, &quot;2017-10-05&quot;, &quot;2014-03-26&quot;, &quot;2016-09-07&quot;, &quot;2018-10-08&quot;, &quot;2015-02-17&quot;, &quot;2017-10-06&quot;, &quot;2014-03-27&quot;, &quot;2016-09-08&quot;))
  </code></pre>
  <p>I would like to be able to simply copy the data from the complete/non-missing rows and paste it into the missing records based on the <code>ID</code> variable, across the entire dataframe.</p>
  <p>I have tried the following methods to no avail (no missing values replaced):</p>
  <pre><code>library(tidyverse)
  library(zoo)
  library(plyr)
  
  # Attempt 1:
  data %&gt;%
    group_by(ID) %&gt;%
    mutate_all(~replace(., is.na(.), .[!is.na(.)][1]))
  
  # Attempt 2:
  plyr::ddply(data, ~ID, na.locf)
  
  </code></pre>
  <p>I've found solutions for cases in which missing values of a single column need to be replaced, but I have not yet found a solution for replacing missing values across an entire dataframe.</p>
  <p>Thanks in advance.</p>