You can use the order()
function directly without resorting to add-on tools -- see this simpler answer which uses a trick right from the top of the example(order)
code:
(您可以直接使用order()
函数,而无需借助附加工具-参见这个更简单的答案,该答案在example(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
Edit some 2+ years later: It was just asked how to do this by column index.
(大约2年后编辑:只是被问到如何通过列索引来做到这一点。)
The answer is to simply pass the desired sorting column(s) to the order()
function: (答案是将所需的排序列简单地传递给order()
函数:)
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>
rather than using the name of the column (and with()
for easier/more direct access).
(而不是使用列名(和with()
可以更容易/更直接地访问)。)
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…