I have a matrix with column names and a vector of names in a different order.
Matrix column names:
c("colname1", "colname2", "colname3", "colname4", "colname5")
Vector of names:
c("colname4", "colname3", "colname2", "colname5", "colname1")
I am trying to order the matrix columns in the same order as the names in the vector.
I have tried:
test <- match(colnames(matrix1), colnames(matrix2))`
but it didn't work. Do you know any alternative?
Index the matrix with the [
-operator and the vector of column names in the desired order:
col.order <- c("colname4","colname3","colname2","colname5","colname1")
M[ , col.order]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With