且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

按多列对数据框行进行排序(排序)

更新时间:2023-01-20 16:56:49

您可以使用 order() 无需借助附加工具即可直接运行 - 请参阅此更简单的答案,该答案使用了 order() 顶部的技巧代码>示例(订单) 代码:

R> dd[with(dd, order(-z, b)), ]
    b x y z
4 Low C 9 2
2 Med D 3 1
1  Hi A 8 1
3  Hi A 9 1

大约 2 年后 只是通过列索引询问如何执行此操作.答案是简单地将所需的排序列传递给 order() 函数:

Edit some 2+ years later: It was just asked how to do this by column index. The answer is to simply pass the desired sorting column(s) to the order() function:

R> dd[order(-dd[,4], dd[,1]), ]
    b x y z
4 Low C 9 2
2 Med D 3 1
1  Hi A 8 1
3  Hi A 9 1
R> 

而不是使用列的名称(和 with() 以便更容易/更直接地访问).

rather than using the name of the column (and with() for easier/more direct access).