title: Set value of one column based on value in another column in R
body: <p>I created a new column with values from another data frame. The problem is that there are empty rows in both data frames. So I assigned incorrectly the names first empty row with the second table for empty rows in first table.</p>
  
  <p>My code:</p>
  
  <pre><code>df1 &lt;- data.frame(age=c(23," ",55,34,45),
                    name=c("A","S","P","J","M"))
  
  
  df2 &lt;- data.frame(age=c(" ",43,55,34,45),
                    name=c("Alex","Silvia","Peter","Jack","Michael"))
  
  
  df1$names2 &lt;- df2$name[match(df1$age, df2$age)]
  
  &gt; df1
    age name  names2
  1  23    A    &lt;NA&gt;
  2        S    Alex
  3  55    P   Peter
  4  34    J    Jack
  5  45    M Michael
  
  </code></pre>
  
  <p>First result record is ok, but the second shows incorrect information.</p>