本文介绍了按R中的多列排序矩阵的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个矩阵
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
我想先按降序对矩阵进行排序,依次按第1列和第2列,得出矩阵
and I would like to order the matrix in descending order first by column 1 and then by column two resulting in the matrix
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.
推荐答案
order
函数应执行此操作.
df[order(df[,1],df[,2],decreasing=TRUE),]
这篇关于按R中的多列排序矩阵的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!