So I have a large dataframe, let's call it df1. Now, I want to replace a subset of rows and columns with values from another dataframe which has exactly the same column names and ids. But the below code isn't producing the results I want and I don't understand why.
df1 <- data.frame(id = c(1:4), a = c("a", "a", "a", "a"), b = c("b", "b", "b", "b"), c = c("c", "c", "c", "c"), d = c("d", "d", "d", "d"))
df2 <- data.frame(id = c(2,3), b = c("x", "x"), c = c("y", "y"))
df1[df2$id, names(df2)] <- df2
I've looked at replace
but don't know how to use it with two data frames. I'm looking for something like merge
which replaces the values.