Reorder Columns in R

There is a very easy way to reorder the column order or a data frame, but it seems to be unknown from many R users.

Having a data frame named test like

     a  b  c  d  e
[1,] 1 11 21 31 41
[2,] 2 12 22 32 42
[3,] 3 13 23 33 43
[4,] 4 14 24 34 44
[5,] 5 15 25 35 45

all that is required is to reassign the data frame using a new column order, like

new_column_order <- c("b", "c", "a", "e", "d")

test <- test[,new_column_order] 

And that solves the problem.

This works both with a data frame and a matrix.