How to sort a matrix by all columns

后端 未结 4 1407
灰色年华
灰色年华 2020-12-15 10:04

Suppose I have

arr = 2 1 3
      1 2 3
      1 1 2

How can I sort this into the below?

arr = 1 1 2
      1 2 3
      2 1 3
         


        
4条回答
  •  隐瞒了意图╮
    2020-12-15 10:28

    I wrote this little func that does decreasing order as well cols allows to choose which columns to order and their order

    ord.mat = function(M, decr = F, cols = NULL){
        if(is.null(cols))
          cols = 1: ncol(M)
        out = do.call( "order", as.data.frame(M[,cols]))
        if (decr)
          out = rev(out)
        return(M[out,])
    }
    

提交回复
热议问题