We’d like to merge some columns from a data frame with the matching columns from various different data frames. Our main data frame predict looks as follows:
>predict
x1 x2 x3
1 1 1
0 1 0
1 1 0
1 1 0
0 0 1
(There may be more columns depending on the quantity of prediction runs)
Our goal is to merge this data frame with the y-columns from three different test data frames (df_1 df_2 and df_3) which all have the same structure. The needed columns are accessed through df_1$y[test]
([test] is a logical vector which identifies the 5 values which match our x-values) and have the same structure as the x-columns from predict.
The desired output would look like this:
>predict_test
x1 x2 x3 y1 y2 y3
1 1 1 1 1 1
0 1 0 0 0 0
1 1 0 0 1 0
1 1 0 1 1 1
0 0 1 0 0 1
In the next step we need to stack the x- and the y- columns into one column in order to do evaluations. It is important to stack them in the correct order, i.e. x2 under x1 and x3 under x2. The y-columns respectively.
>predict_test_stack
x_all y_all
1 1
0 0
1 0
1 1
0 0
1 1
1 0
1 1
1 1
0 0
1 1
0 0
0 0
0 1
1 1
This probably works with melt
, but we don't know how to apply it while indicating two different id variables.
Thanks for your help.