-7

I have 4 data frames all with the same number of columns and identical column names.
The order of the columns is different.
I want to combine all 4 data frames together and match them with the column name.

Balan
  • 421
  • 6
  • 12
Chad Coleman
  • 69
  • 1
  • 8
  • 1
    check out `merge` – holzben Oct 14 '16 at 14:19
  • 1
    Which data frame has the desired order? Or does it not matter? A reproducible example with a few things that you tried would help. btw 'merge' is not the right function for this as there are no key columns. – Pierre L Oct 14 '16 at 14:20
  • Take a look at `rbind` – ytk Oct 14 '16 at 14:34
  • 2
    Here you have how to make a great R reproducible example when you want an answer. http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example – Enrique Oct 14 '16 at 14:41

2 Answers2

0

Working Azure ML - This was the best option I found to automate this merge.

df <- maml.mapInputPort(1)
df2 <- maml.mapInputPort(2) 

if (length(df2.toAdd <- setdiff (names(df), names(df2))))
df2[, c(df2.toAdd) := NA]

if (length(df.toAdd <- setdiff (names(df2), names(df))))
df[, c(df.toAdd) := NA]

df3 <- rbind(df, df2, use.names=TRUE)


maml.mapOutputPort("df3");
Chad Coleman
  • 69
  • 1
  • 8
0

Suppose your 4 data frames are named df1, df2, df3 and df4, since the number of columns and the column names are identical, then why not this:

cl <- sort(colnames(df1))
mrg <- rbind(df1[,cl], df2[,cl], df3[,cl], df4[,cl])

If you want to have them in a specific order of columns, for example the order of columns in df2, then you can do this:

mrg <- mrg[,colnames(df2)]
989
  • 12,579
  • 5
  • 31
  • 53