title: Iterate from one data frame to another, error
body: <p>I want to fill the NA in df with the values in data frame dat and iterate over columns, but it doesn't work, why? Or is there a better solution?</p>
  
  <pre><code>id &lt;- factor(rep(letters[1:2], each=5))
  A &lt;- c(1,2,NA,6,8,9,0,6,7,9)
  B &lt;- c(5,6,1,9,8,1,NA,9,7,4)
  C &lt;- c(2,3,5,NA,NA,2,7,6,4,6)
  D &lt;- c(6,5,8,3,2,9,NA,2,6,8)
  df &lt;- data.frame(id, A, B,C,D) 
  df
  
     id  A  B  C  D
  1   a  1  5  2  6
  2   a  2  6  3  5
  3   a NA  1  5  8
  4   a  6  9 NA  3
  5   a  8  8 NA  2
  6   b  9  1  2  9
  7   b  0 NA  7 NA
  8   b  6  9  6  2
  9   b  7  7  4  6
  10  b  9  4  6  8
  
  dat &lt;- data.frame(col=c("A","B","C","D"), value=c(23,45,26,89))
  dat
  
   col value
  1   A    23
  2   B    45
  3   C    26
  4   D    89
  
  test &lt;- function(i){
    df[,i][is.na(df[,i])] &lt;- dat$value[dat$col==i]
    return(df)
  }
  df &lt;-df[,-1]
  
  for(i in colnames(df)){
    df[[i]] &lt;- test(i)
  }
  df #DOESN'T WORK
  </code></pre>
  
  <p>Should look like:</p>
  
  <pre><code> df
     id  A  B  C  D
  1   a  1  5  2  6
  2   a  2  6  3  5
  3   a 23  1  5  8
  4   a  6  9 26  3
  5   a  8  8 26  2
  6   b  9  1  2  9
  7   b  0 45  7 89
  8   b  6  9  6  2
  9   b  7  7  4  6
  10  b  9  4  6  8
  </code></pre>