在R中,如何通过一个降序和一个升序来sortingdataframe?

我有一个数据框,看起来像这样:

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进行sorting,对I1中I2值相同的行进行升序排列,得到行数为1 3 4 2 。 但是order函数似乎只有一个decreasing参数,对于所有的sorting向量一次是TRUEFALSE 。 我如何得到我的sorting正确的?

我用这个代码来产生你想要的输出。 这是你以后?

 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 

恐怕罗曼·卢斯特里克的回答是错误的。 它偶然地在这个input上工作。 例如,考虑在一个非常相似的input上输出它的输出(附加一行类似于I2列中“c”的原始行3):

 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 1 5 6 55 48 60 6 c"), 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 3 1 5 6 55 48 60 6 f 1 2 3 5 52 43 61 6 b 5 1 5 6 55 48 60 6 c 4 2 4 4 65 64 58 2 b 2 6 4 3 72 NA 59 1 a 

这不是理想的结果:I2的前三个值是fbc而不是bcf ,这是预期的,因为次级sorting是I2按升序排列。

为了得到I2的相反顺序,您希望较大的值较小,反之亦然。 对于数字值乘以-1将做到这一点,但对于字符它有点棘手。 字符/string的一般解决scheme是通过因素,颠倒水平(使大的值小,小的值大),并将因素改回字符:

 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 1 5 6 55 48 60 6 c"), header = TRUE) f=factor(rum$I2) levels(f) = rev(levels(f)) rum[order(rum$I1, as.character(f), decreasing = TRUE), ] P1 P2 P3 T1 T2 T3 I1 I2 1 2 3 5 52 43 61 6 b 5 1 5 6 55 48 60 6 c 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 

我用rank

 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 1 5 6 55 48 60 6 c"), header = TRUE) > rum[order(rum$I1, -rank(rum$I2), decreasing = TRUE), ] P1 P2 P3 T1 T2 T3 I1 I2 1 2 3 5 52 43 61 6 b 5 1 5 6 55 48 60 6 c 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 

假设df是具有2个字段A和B的dataframe

情况1:如果您的字段A和B是数字

df[order(df[,1],df[,2]),] - sorts fields A and B in ascending order
df[order(df[,1],-df[,2]),] - sorts fields A in ascending and B in descending order
优先考虑A.

情况2:如果字段A或B是非数字说话因素或字符

在我们的情况下,如果B是字符,我们要按相反的顺序sorting
df[order(df[,1],-as.numeric(as.factor(df[,2]))),] -> this sorts field A(numerical) in ascending and field B(character) in descending.
优先考虑A.

The idea is that you can apply -sign in order function ony on numericals. So for sorting character strings in descending order you have to coerce them to numericals.

默认sorting是稳定的,所以我们sorting两次:首先由小键,然后由大键

 rum1 <- rum[order(rum$I2, decreasing = FALSE),] rum2 <- rum1[order(rum1$I1, decreasing = TRUE),] 
  library(dplyr) library(tidyr) #supposing you want to arrange column 'c' in descending order and 'd' in ascending order. name of data frame is df ## first doing descending df<-arrange(df,desc(c)) ## then the ascending order of col 'd; df <-arrange(df,d) 

正确的方法是:

 rum[order(rum$T1, rum$T2, decreasing=c(T,F)), ] 
 rum[order(rum$T1, -rum$T2 ), ] 

在@ dudusan的例子中,你也可以颠倒I1的顺序,然后按升序sorting:

 > 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 + 1 5 6 55 48 60 6 c"), header = TRUE) > f=factor(rum$I1) > levels(f) <- sort(levels(f), decreasing = TRUE) > rum[order(as.character(f), rum$I2), ] P1 P2 P3 T1 T2 T3 I1 I2 1 2 3 5 52 43 61 6 b 5 1 5 6 55 48 60 6 c 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 > 

这似乎有点短,你不要两次颠倒I2的顺序。

简单的一个没有等级:

 rum[order(rum$I1, -rum$I2, decreasing = TRUE), ]