title: Is there a way to replace rows in one dataframe with another in R?
body: <p>I'm trying to figure out how to replace rows in one dataframe with another by matching the values of one of the columns. Both dataframes have the same column names. </p>
  
  <p>Ex: </p>
  
  <pre><code>df1 &lt;- data.frame(x = c(1,2,3,4), y = c("a", "b", "c", "d"))
  
  df2 &lt;- data.frame(x = c(1,2), y = c("f", "g"))
  </code></pre>
  
  <p>Is there a way to replace the rows of df1 with the same row in df2 where they share the same x variable? It would look like this. </p>
  
  <pre><code>data.frame(x = c(1,2,3,4), y = c("f","g","c","d")
  </code></pre>
  
  <p>I've been working on this for a while and this is the closest I've gotten - </p>
  
  <pre><code>df1[which(df1$x %in% df2$x),]$y &lt;- df2[which(df1$x %in% df2$x),]$y
  </code></pre>
  
  <p>But it just replaces the values with NA.</p>
  
  <p>Does anyone know how to do this? </p>