I have an issue merging two datasets of different lengths - below are some examples
df1:
A B C D E F
1 1 1 10 20 3
1 1 1 10 20 2
1 1 1 15 10 1
1 1 1 20 15 3
1 2 1 10 20 3
1 2 1 15 10 3
2 1 1 20 20 3
2 1 1 30 10 1
2 2 1 50 10 2
df2:
A B C X Y Z
1 1 1 10 10 1
1 2 1 20 20 2
2 1 1 10 20 1
2 2 1 20 20 2
I need to create a new dataframe where I incorporate df2$X
and df2$Y
into df1 for each grouping variable (A and B).
I have tried the following:
df3 <- merge(df2, df1, all.y = TRUE)
This works as far df3$X
and df3$Y
are concerned, however the ordering of df3$D
and df3$E
is slightly wrong.
Do you have any suggestions as to how I can keep the ordering for these variables correct as they first appear in df1?