Sort matrix according to first column in R

后端 未结 5 1380
抹茶落季
抹茶落季 2020-12-07 17:50

I have a matrix with two columns of the following form:

1 349
1 393
1 392
4 459
3 49
3 32
2 94

I would like to sort this matrix in increasi

5条回答
  •  萌比男神i
    2020-12-07 18:13

    Be aware that if you want to have values in the reverse order, you can easily do so:

    > example = matrix(c(1,1,1,4,3,3,2,349,393,392,459,49,32,94), ncol = 2)
    > example[order(example[,1], decreasing = TRUE),]
         [,1] [,2]
    [1,]    4  459
    [2,]    3   49
    [3,]    3   32
    [4,]    2   94
    [5,]    1  349
    [6,]    1  393
    [7,]    1  392
    

提交回复
热议问题