title: How to replace NA values by matching on ID, with two different data frames
body: <p>I have two data frames, and I want to transfer the latitude and longitude from one df to another. DF1 is missing most of the lat and long, but DF2 also has a few missing values in the lat and long tool. I want to match on their location id (loc_id), but not all rows in DF1 have a loc_id.</p>
  
  <pre><code>reg_id &lt;- c(1,2,3,4,5,6,7)
  loc_id &lt;- c(NA,1,1,NA,2,3,4)
  lat &lt;- c(NA,12.56,12.56,NA,NA,13.757,NA)
  long &lt;- c(NA,-31.2,-31.2,NA,NA,-56.43,NA)
  DF1 &lt;- data.frame(reg_id,loc_id,lat,long)
  
  loc_id &lt;- c(1,2,3,4,5,60)
  loc_lat &lt;- c(12.56,20.7,13.757,NA,20.3,50.7)
  loc_long &lt;- c(-31.2,-47.3,-56.43,NA,-71.123,-27.1)
  DF2 &lt;- data.frame(loc_id,loc_lat,loc_long)
  </code></pre>
  
  <p>Goal is to end up with a df like this:</p>
  
  <pre><code>reg_id   loc_id   lat       long
    1        NA      NA        NA
    2        1      12.56     -31.2
    3        1      12.56     -31
    4        NA      NA        NA
    5        2      20.7      -47.3
    6        3      13.757   -56.43
    7        4       NA        NA
  </code></pre>
  
  <p>Note: The lat and long are not the same lengths for each row.</p>
  
  <p>Thank you for your help!</p>
  
  <p>Just some background, I've tried match, <code>setDT</code>, <code>merge</code>, to no avail.</p>
  
  <pre><code>no_ll_DF1 &lt;- which(is.na(DF1$lat) &amp; !is.na(DF1$loc_id))
  yes_ll_DF2 &lt;- which(!is.na(DF2$loc_lat))
  
  DF1$lat[match(DF2$loc_id[yes_ll_DF2], DF1$loc_id[no_ll_DF1])] &lt;- DF2$loc_lat[yes_ll_DF2]
  </code></pre>
  
  <p>When using match, I would get this error:</p>
  
  <blockquote>
    <p>Error in DF1$lat[match(DF2$loc_id[yes_ll_DF2], DF1$loc_id[no_ll_DF1])] &lt;- DF2$loc_lat[yes_ll_DF2] : 
      NAs are not allowed in subscripted assignments</p>
  </blockquote>