如何在数据集中对多个因子列进行排序/排序

时间:2016-05-11 19:08:48

标签: r sorting dataframe

我想一次排序/订购多个因子列(食物和组)。我希望所有非CRISPR都在最顶层,在食物专栏中我想在非CRISPR活动中定期跟随尼古丁。这是我的代码。如果我通过f1订购,我得到下面的结果,如果我订购f我得到所有常规跟随尼古丁。我怎样才能得到这两个?

        GenoSDs1<- aggregate( Viability ~ Line+Food+Group, x, sd );
        f=factor(GenoSDs1$Food)
        f1<-factor(GenoSDs1$Group)
        levels(f) = rev(levels(f))
        levels(f1) = rev(levels(f1))
        GenoSDs1[order(GenoSDs1$Food, as.character(f1), decreasing = TRUE), ]
        GenoSDs1

         Line     Food     Group  
        33 11m3m Nicotine NonCRISPR 
        34 13f2m Nicotine NonCRISPR 
        35 13f5f Nicotine NonCRISPR 
        36 13m5f Nicotine NonCRISPR         
        37  1f1f Nicotine NonCRISPR 
        38  2f4f Nicotine NonCRISPR 
        39  3m5f Nicotine NonCRISPR 
        40  7f4m Nicotine NonCRISPR 
        41 11m3m  Regular NonCRISPR 
        42 13f2m  Regular NonCRISPR 
        43 13f5f  Regular NonCRISPR 
        44 13m5f  Regular NonCRISPR         
        45  1f1f  Regular NonCRISPR 
        46  2f4f  Regular NonCRISPR 
        47  3m5f  Regular NonCRISPR 
        1  13f3f Nicotine    CRISPR 
        2  13m2m Nicotine    CRISPR 
        3  13m3f Nicotine    CRISPR 
        4  13m4m Nicotine    CRISPR 
        6  13f3f Regular    CRISPR 
        7  13m2m Regular    CRISPR 

这就是我想要的。

    Line    Food    Group
    41  11m3m   Regular NonCRISPR
    42  13f2m   Regular NonCRISPR
    43  13f5f   Regular NonCRISPR
    44  13m5f   Regular NonCRISPR
    45  1f1f    Regular NonCRISPR
    46  2f4f    Regular NonCRISPR
    47  3m5f    Regular NonCRISPR
    48  7f4m    Regular NonCRISPR
    33  11m3m   Nicotine    NonCRISPR
    34  13f2m   Nicotine    NonCRISPR
    35  13f5f   Nicotine    NonCRISPR
    36  13m5f   Nicotine    NonCRISPR
    37  1f1f    Nicotine    NonCRISPR
    38  2f4f    Nicotine    NonCRISPR
    39  3m5f    Nicotine    NonCRISPR
    40  7f4m    Nicotine    NonCRISPR
    6  13f3f Regular    CRISPR 
    7  13m2m Regular    CRISPR 
    1   13f3f   Nicotine    CRISPR
    2   13m2m   Nicotine    CRISPR
    3   13m3f   Nicotine    CRISPR
    4   13m4m   Nicotine    CRISPR

2 个答案:

答案 0 :(得分:0)

怎么样:

GenoSDs1[with(GenoSDs1,order(-Group,-Food)),]

答案 1 :(得分:0)

OpenCV Error: Assertion failed (data.dims <= 2 && type == CV_32F && K > 0)

在Base R

tbl <- read.table(text = "
Line     Food     Group  
33 11m3m Nicotine NonCRISPR 
34 13f2m Nicotine NonCRISPR 
35 13f5f Nicotine NonCRISPR 
36 13m5f Nicotine NonCRISPR         
37  1f1f Nicotine NonCRISPR 
38  2f4f Nicotine NonCRISPR 
39  3m5f Nicotine NonCRISPR 
40  7f4m Nicotine NonCRISPR 
41 11m3m  Regular NonCRISPR 
42 13f2m  Regular NonCRISPR 
43 13f5f  Regular NonCRISPR 
44 13m5f  Regular NonCRISPR         
45  1f1f  Regular NonCRISPR 
46  2f4f  Regular NonCRISPR 
47  3m5f  Regular NonCRISPR 
1  13f3f Nicotine    CRISPR 
2  13m2m Nicotine    CRISPR 
3  13m3f Nicotine    CRISPR 
4  13m4m Nicotine    CRISPR 
6  13f3f Regular    CRISPR 
7  13m2m Regular    CRISPR 
")
tbl$Food <- factor(trimws(tbl$Food), levels = c("Regular", "Nicotine"), ordered=TRUE)
tbl$Group <- factor(trimws(tbl$Group), levels = c("NonCRISPR", "CRISPR"), ordered=TRUE)

require(dplyr)
tbl %>% arrange (Group, Food, Line)

Line     Food     Group
1  11m3m  Regular NonCRISPR
2  13f2m  Regular NonCRISPR
3  13f5f  Regular NonCRISPR
4  13m5f  Regular NonCRISPR
5   1f1f  Regular NonCRISPR
6   2f4f  Regular NonCRISPR
7   3m5f  Regular NonCRISPR
8  11m3m Nicotine NonCRISPR
9  13f2m Nicotine NonCRISPR
10 13f5f Nicotine NonCRISPR
11 13m5f Nicotine NonCRISPR
12  1f1f Nicotine NonCRISPR
13  2f4f Nicotine NonCRISPR
14  3m5f Nicotine NonCRISPR
15  7f4m Nicotine NonCRISPR
16 13f3f  Regular    CRISPR
17 13m2m  Regular    CRISPR
18 13f3f Nicotine    CRISPR
19 13m2m Nicotine    CRISPR
20 13m3f Nicotine    CRISPR
21 13m4m Nicotine    CRISPR