title: Merge a single column in two data frames in R where only some rows match
body: <p>I'm trying to merge data from a column in one data frame into another column in a second data frame, using a column with shared data as the key for the operation. I've tried a couple of different tutorials on how to replicate Excel vlookup in r, and I've tried several different methods of merger provided here in answers on stackoverflow...but so far nothing I've tried does exactly what I'm trying to do, even though it seems like it would be fairly common function.</p>
  
  <p>So here's what I'm trying to do...to provide a very simplistic example, let's say this is dataframe1:</p>
  
  <pre class="lang-html prettyprint-override"><code>ID      RESULT
  
  4       YES
  1       YES
  3       NO
  2       YES
  </code></pre>
  
  
  
  <p>And below is dataframe2, where the RESULT column is entirely populated by NAs, some of which will be replaced by what appears in dataframe1$RESULT (there are many more values in dataframe2$ID than in dataframe1$ID, so many NAs should remain after the merge):</p>
  
  <pre class="lang-html prettyprint-override"><code>ID      RESULT
  
  1       NA
  8       NA
  2       NA
  9       NA
  3       NA
  6       NA
  7       NA
  4       NA
  </code></pre>
  
  
  
  <p>So the end result that I want is below, dataframe3, where RESULT contains YES or NO, if one of those values were present in dataframe1$RESULT, or NA where there was no match between dataframe1$ID and dataframe2$ID.</p>
  
  <pre class="lang-html prettyprint-override"><code>ID      RESULT
  
  1       YES
  8       NA
  2       YES
  9       NA
  3       NO
  6       NA
  7       NA
  4       YES
  </code></pre>
  
  
  
  <p>I've managed to do a merge with the following that adds a column (RESULT.y) to dataframe1, but it also removes all of the rows from dataframe2 that don't have a match in dataframe1:</p>
  
  <pre class="lang-html prettyprint-override"><code>dataframe3 &lt;- merge(dataframe2, dataframe1, by=c("ID"))
  </code></pre>
  
  
  
  <p>What I end up with is:</p>
  
  <pre class="lang-html prettyprint-override"><code>    ID      RESULT.x  RESULT.y
  
      4       NA        YES
      1       NA        YES
      3       NA        NO
      2       NA        YES
  </code></pre>
  
  
  
  <p>If it was JUST that it added a new column instead of modifying the existing one, I could work around that, but I can't figure out how to get it to leave the other rows alone rather than eliminating them. I tried the following, but it returns an error:</p>
  
  <pre class="lang-html prettyprint-override"><code>dataframe3 &lt;- merge(dataframe2, dataframe1, by=c("ID", all.x = TRUE))
  
  Error in fix.by(by.x, x) : 'by' must specify a uniquely valid column
  </code></pre>
  
  
  
  <p>Help will be very, very much appreciated...I've only been working in r for a couple weeks.</p>