用r中的多列来排列矩阵。

时间:2021-10-28 22:57:12

I have a matrix

我有一个矩阵

df<-matrix(data=c(3,7,5,0,1,0,0,0,0,8,0,9), ncol=2)
rownames(df)<-c("a","b","c","d","e","f")

[,1] [,2]
a    3    0
b    7    0
c    5    0
d    0    8
e    1    0
f    0    9

and I would like to order the matrix in descending order first by column 1 and then by column two resulting in the matrix

我想把矩阵按降序排列首先按列1,然后再按列2,得到矩阵

df.ordered<-matrix(data=c(7,5,3,1,0,0,0,0,0,0,9,8),ncol=2)
rownames(df.ordered)<-c("b","c","a","e","f","d")

   [,1] [,2]
 b    7    0
 c    5    0
 a    3    0
 e    1    0
 f    0    9
 d    0    8

Any suggestions on how I could achieve this? Thanks.

有什么建议吗?谢谢。

3 个解决方案

#1


24  

The order function should do it.

顺序函数应该这样做。

df[order(df[,1],df[,2],decreasing=TRUE),]

#2


13  

To complete the main answer, here is a way to do it programmatically, without having to specify the columns by hand:

要完成主要的答案,这里有一种编程的方法,无需手工指定列:

set.seed(2013) # preparing my example
mat <- matrix(sample.int(10,size = 30, replace = T), ncol = 3)
mat
      [,1] [,2] [,3]
 [1,]    5    1    6
 [2,]   10    3    1
 [3,]    8    8    1
 [4,]    8    9    9
 [5,]    3    7    3
 [6,]    8    8    5
 [7,]   10   10    2
 [8,]    8   10    7
 [9,]   10    1    9
[10,]    9    4    5

As a simple example, let say I want to use all the columns in their order of appearance to sort the rows of the matrix: (One could easily give a vector of indexes to the matrix)

作为一个简单的例子,假设我想使用所有列按其外观顺序对矩阵的行进行排序:(可以很容易地为矩阵提供一个索引向量)

mat[do.call(order, as.data.frame(mat)),]   #could be ..as.data.frame(mat[,index_vec])..
      [,1] [,2] [,3]
 [1,]    3    7    3
 [2,]    5    1    6
 [3,]    8    8    1
 [4,]    8    8    5
 [5,]    8    9    9
 [6,]    8   10    7
 [7,]    9    4    5
 [8,]   10    1    9
 [9,]   10    3    1
[10,]   10   10    2

#3


4  

order function will help you out, try this:

订单功能可以帮你解决,试试这个:

df[order(-df[,1],-df[,2]),] 
  [,1] [,2]
b    7    0
c    5    0
a    3    0
e    1    0
f    0    9
d    0    8

The minus before df indicates that the order is decreasing. You will get the same result setting decreasing=TRUE.

df之前的-表示顺序是递减的。您将得到相同的结果设置reduce =TRUE。

df[order(df[,1],df[,2],decreasing=TRUE),]

#1


24  

The order function should do it.

顺序函数应该这样做。

df[order(df[,1],df[,2],decreasing=TRUE),]

#2


13  

To complete the main answer, here is a way to do it programmatically, without having to specify the columns by hand:

要完成主要的答案,这里有一种编程的方法,无需手工指定列:

set.seed(2013) # preparing my example
mat <- matrix(sample.int(10,size = 30, replace = T), ncol = 3)
mat
      [,1] [,2] [,3]
 [1,]    5    1    6
 [2,]   10    3    1
 [3,]    8    8    1
 [4,]    8    9    9
 [5,]    3    7    3
 [6,]    8    8    5
 [7,]   10   10    2
 [8,]    8   10    7
 [9,]   10    1    9
[10,]    9    4    5

As a simple example, let say I want to use all the columns in their order of appearance to sort the rows of the matrix: (One could easily give a vector of indexes to the matrix)

作为一个简单的例子,假设我想使用所有列按其外观顺序对矩阵的行进行排序:(可以很容易地为矩阵提供一个索引向量)

mat[do.call(order, as.data.frame(mat)),]   #could be ..as.data.frame(mat[,index_vec])..
      [,1] [,2] [,3]
 [1,]    3    7    3
 [2,]    5    1    6
 [3,]    8    8    1
 [4,]    8    8    5
 [5,]    8    9    9
 [6,]    8   10    7
 [7,]    9    4    5
 [8,]   10    1    9
 [9,]   10    3    1
[10,]   10   10    2

#3


4  

order function will help you out, try this:

订单功能可以帮你解决,试试这个:

df[order(-df[,1],-df[,2]),] 
  [,1] [,2]
b    7    0
c    5    0
a    3    0
e    1    0
f    0    9
d    0    8

The minus before df indicates that the order is decreasing. You will get the same result setting decreasing=TRUE.

df之前的-表示顺序是递减的。您将得到相同的结果设置reduce =TRUE。

df[order(df[,1],df[,2],decreasing=TRUE),]