Order columns in a matrix

  Kiến thức lập trình

I have a matrix mat. I would like to re-order the columns, using column name:

mat <-matrix(1:12,3)
colnames(mat) <-  c("I to I",   "I to II",  "I to III", "I to IV")  

ordered_mat <- cbind(mat[,"I to IV"],mat[,"I to II"],mat[,"I to I"],mat[,"I to III"])

This works, however, the column name disappeared and now I just have V1,V2…,V4, instead. Is there a better way to do this, while preserving the column names?

Theme wordpress giá rẻ Theme wordpress giá rẻ Thiết kế website

LEAVE A COMMENT