如何按一降一升列排序数据帧? [英] How to order a data frame by one descending and one ascending column?
本文介绍了如何按一降一升列排序数据帧?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个数据框,看起来像这样:
I have a data frame, which looks like that:
P1 P2 P3 T1 T2 T3 I1 I2
1 2 3 5 52 43 61 6 "b"
2 6 4 3 72 NA 59 1 "a"
3 1 5 6 55 48 60 6 "f"
4 2 4 4 65 64 58 2 "b"
我想按I1对其进行降序排序,并在I1中将具有相同值的行按I2进行升序排序,并按1 3 4 2
顺序获取行.但是order
函数似乎只接受一个decreasing
参数,然后对于所有排序向量一次是TRUE
或FALSE
.如何正确排序?
I want to sort it by I1 in descending order, and rows with the same value in I1 by I2 in ascending order, getting the rows in the order 1 3 4 2
. But the order
function seems to only take one decreasing
argument, which is then TRUE
or FALSE
for all ordering vectors at once. How do I get my sort correct?
推荐答案
我使用此代码生成所需的输出.这是你所追求的吗?
I used this code to produce your desired output. Is this what you were after?
rum <- read.table(textConnection("P1 P2 P3 T1 T2 T3 I1 I2
2 3 5 52 43 61 6 b
6 4 3 72 NA 59 1 a
1 5 6 55 48 60 6 f
2 4 4 65 64 58 2 b"), header = TRUE)
rum$I2 <- as.character(rum$I2)
rum[order(rum$I1, rev(rum$I2), decreasing = TRUE), ]
P1 P2 P3 T1 T2 T3 I1 I2
1 2 3 5 52 43 61 6 b
3 1 5 6 55 48 60 6 f
4 2 4 4 65 64 58 2 b
2 6 4 3 72 NA 59 1 a
这篇关于如何按一降一升列排序数据帧?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文