问题
Sorry if this is very basic. I have a list of names and a matrix with those names as column names. However, the colnames are in a different order.
Eg. List of names: colname4 colname3 colname2 colname5 colname1
Matrix Colnames: colname1 colname2 colname3 colname4 colname5
I am trying to order the matrix columns in the same order as list of names order.
I have tried test <- match(colnames(matrix1), colnames(matrix2))
but it didn't work. Do you know any alternative?
回答1:
You just have to use a vector for the names and the [
-operator as follows:
col.order <- c("colname4","colname3","colname2","colname5","colname1")
M[,col.order]
来源:https://stackoverflow.com/questions/25446714/r-reorder-matrix-columns-by-matching-colnames-to-list-of-string