Say I have df1 and df2. I want to replace all elements from df2$X2 that correspond to df1$X1 into df1$X2. My attempt does the replacements, but not in the way I would like. I am ultimtely trying to get df1 to look like df3. Any suggestions?
df1 <- data.frame(cbind(c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4, 5, 5, 5, 5), NA))
df2 <- data.frame(cbind(c(1, 4, 5), c("A", "B", "C")))
df1$X2 <- replace(df1$X2, df1$X1 %in% df2$X1, df2$X2)
df3 <- structure(list(X1 = c(1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4,
4, 4, 4, 5, 5, 5, 5), X2 = c("A", "A", "A", "A", NA, NA, NA,
NA, NA, NA, NA, NA, "B", "B", "B", "B", "C", "C", "C", "C")), row.names = c(NA,
-20L), class = "data.frame")