I have two dataframes df1 and df2. In my real dataframes, I have indices, so I added them here in the reproducible example too.
object <- c("x1", "x2", "x3")
color <- c("red", "green", "blue")
shape <- c("square", "circle", "square")
df1 <- data.frame(object, color, shape, row.names = NULL)
df1
object color shape
1 x1 red square
2 x2 green circle
3 x3 blue square
param <- c("weight", "len", "volume")
x1 <- c("value", "value", "value")
x2 <- c("value", "value", "value")
x3 <- c("value", "value", "value")
x4 <- c("value", "value", "value")
x5 <- c("value", "value", "value")
df2 <- data.frame(param, x1, x2, x3, x4, x5, row.names = NULL)
df2
param x1 x2 x3 x4 x5
1 weight value value value value value
2 len value value value value value
3 volume value value value value value
df1's object column has the same values and the df2's column names (except the param column). df2 has more X columns than the number of df1's rows. x1, x2, x3, ... orders do not match in two data frames. I need to combine two datasets matching x's and get something like this:
x1 x2 x3
weight value value value
len value value value
volume value value value
color red green blue
shape square circle square