Reorder multiple columns of only hundreds

What is the most efficient way to reorder columns for data frames with hundreds and thousands of columns?

Tries and works, but I'm looking for the optimal function.

dput(names(df1))

to dump all column names and be used in the next step

swapped columns with df1[c(col1, col3, col2,.....col99,col100)

ex names(df1)
"col1" "col2" "col3".............."col99" "col100"

      

Want to exchange only "col2" and "col3",

names(df1)
"col1" "col3" "col2".............."col99" "col100"

      

+3


source to share


1 answer


df1 = df1[,c(1,3,2,4:100)]

      



reorders your columns according to the permutation c(1,3,2,4:100)

+4


source







All Articles