I have two dataframes, each with the same columns. Some columns have the same values in the same order in both dataframes (X1, X2 below). Other columns have the same values, but in a different order (Y1). This is only a problem for some levels of first variables (here, the order of rows in Y1 differs for X1 == "a", but not X1 == "b"). Example:
df1 <- data.frame("X1" = c("a", "a", "a", "b", "b", "b"),
"X2" = c("1", "2", "3", "1", "2", "3"),
"Y1" = c("d", "d", "f", "g", "h", "i"))
df2 <- data.frame("X1" = c("a", "a", "a", "b", "b", "b"),
"X2" = c("1", "2", "3", "1", "2", "3"),
"Y1" = c("f", "d", "d", "g", "h", "i"))
I would like to change the values of df2$X1 and df2$X2 such that the two dataframes are matched on values of Y1.
I would like to change X1 and X2 rather than Y1 because there are many Y variables. I would like to do this only for df$X1 == "a".
The output should looks like this:
df2 <- data.frame("X1" = c("a", "a", "a", "b", "b", "b"),
"X2" = c("3", "1", "2", "1", "2", "3"),
"Y1" = c("f", "d", "d", "g", "h", "i"))
dforY1which correspond to different values ofX2)