-1

I have a R dataframe with column orders as follows

      Name,ID,Class,Division

I have another dataframe with same columns but,with different order.

     ID,Class,Division,Name

I want above dataframe column orders to be same as that of the first one. How can I achieve this in R?

Neil
  • 7,937
  • 22
  • 87
  • 145
  • 1
    In my opinion, it's better just to `rbind` the two tables and avoid worrying about incongruent column orders. – Frank Apr 10 '17 at 19:23

1 Answers1

3

We can order the second dataframe columns using the column names of the first (assuming both of them have the same column names)

df2[names(df1)]

If it is a data.table, use setcolorder

library(data.table)
setcolorder(df2, names(df1))
akrun
  • 874,273
  • 37
  • 540
  • 662