如何按所有列对矩阵排序 [英] How to sort a matrix by all columns

查看:85
本文介绍了如何按所有列对矩阵排序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有

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

也就是说,首先是第一列,然后是第二列,依此类推.

That is, first by column one, then by column two etc.

推荐答案

这将起作用:

arr[do.call(order, lapply(1:NCOL(arr), function(i) arr[, i])), ]

它正在做什么:

arr[order(arr[, 1], arr[, 2], arr[ , 3]), ]

除了它允许在矩阵中任意数量的列.

except it allows an arbitrary number of columns in the matrix.

这篇关于如何按所有列对矩阵排序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆