I have several dataframes that contain largely the same variables, but some variables are missing from certain dataframes. I would like to cbind (one particular column of*) the dataframes while creating NA values in the missing fields. By way of example:
d1 <- data.frame(y1 = c("var1", "var2", "var3"),
y2 = c(3, 2, 4),
y3 = c("not_needed", "not_needed2", "not_needed3"))
d2 <- data.frame(y1 = c("var1", "var3"),
y2 = c(2, 1),
y3 = c("not_needed", "not_needed2"))
d3 <- data.frame(y1 = c("var1", "var2", "var4"),
y2 = c(3, 2, 5),
y3 = c("not_needed", "not_needed2", "not_needed3"))
expected_output <- data.frame(y1 = c("var1", "var2", "var3", "var4"),
y2.d1 = c(3, 2, 4, NA),
y2.d2 = c(2, NA, 1, NA),
y2.d3 = c(3, 2, NA, 5))
*The column y3
is not required in the output dataframe.
I've experimented with rbind.fill()
from plyr
and a few other ideas, but no success so far.
@joran I don't believe that this is a duplicate of the linked question, as I'm not trying to merge the entire dataframes, just one column from each. I appreciate that the answer is probably in there somewhere, but it's not specifically mentioned.