Sort matrix according to first column in R

后端 未结 5 1387
抹茶落季
抹茶落季 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条回答
  •  长情又很酷
    2020-12-07 18:16

    Read the data:

    foo <- read.table(text="1 349
      1 393
      1 392
      4 459
      3 49
      3 32
      2 94")
    

    And sort:

    foo[order(foo$V1),]
    

    This relies on the fact that order keeps ties in their original order. See ?order.

提交回复
热议问题