If I have a martix that looks like this:
Name <- list("A","B","C", "D")
mat <- matrix(1:4, ncol = 4)
colnames(mat) <- Name
mat
which produces:
A B C D
[1,] 1 2 3 4
And I have a vector like the:
nameOrder <- c("B","D","A","C")
Is there a way to reorder the matrix columns so that the order matches that of the vector nameOrder
? That is, the result would look something like this:
mat_1 <- reorder(mat columns to = nameOrder)
mat_1
B D A C
[1,]2 4 1 3