按数据框的第一列排序列表

时间:2016-05-13 16:05:06

标签: r list sorting dataframe

我想通过数据框的列对列表进行排序。与列表相比,数据帧的长度不相等,如果您将数据帧列唯一,则它将与列表具有相同的长度。我的数据帧是GenoMeans1,我的列表是Genodata。我想按照GenoMEANs1的顺序对列表进行排序。这样我可以做一个t.test,它对应于比较Regular和Nicotine的唯一Line,并按照平均值的顺序绘制。由于出版原因,我离开了Viability。最后,您将看到期望输出。我尝试了几个没有效果的想法。有人能帮忙吗?我

## you will use the dataframe labeled data ## 
GenoMEANs1<- aggregate( Viability ~ Line+Food+Group, data, mean );
f<-factor(GenoMEANs1$Food)
f1<-factor(GenoMEANs1$Group)
levels(f) <- rev(levels(f))
evels(f) <- rev(levels(f1))
GenoMEANs1<-GenoMEANs1[order(GenoMEANs1$Food, as.character(f1), decreasing = TRUE), ]

GenoMEANs1

Line    Food    Group
11m3m   Regular NonCRISPR
13f2m   Regular NonCRISPR
13f5f   Regular NonCRISPR
1f1f    Regular NonCRISPR
2f4f    Regular NonCRISPR
3m5f    Regular NonCRISPR
7f4m    Regular NonCRISPR
13f3f   Regular CRISPR
13m2m   Regular CRISPR
13m3f   Regular CRISPR
13m4m   Regular CRISPR
13m5f   Regular CRISPR
19f4f   Regular CRISPR
1f2f    Regular CRISPR
1m2f    Regular CRISPR
1m2m    Regular CRISPR
3f4f    Regular CRISPR
6f1m    Regular CRISPR
6m3m    Regular CRISPR
6m4f    Regular CRISPR
7f4f    Regular CRISPR
8f2f    Regular CRISPR
8f3m    Regular CRISPR
11m3m   Nicotine    NonCRISPR
13f2m   Nicotine    NonCRISPR
13f5f   Nicotine    NonCRISPR
1f1f    Nicotine    NonCRISPR
2f4f    Nicotine    NonCRISPR
3m5f    Nicotine    NonCRISPR
7f4m    Nicotine    NonCRISPR
13f3f   Nicotine    CRISPR
13m2m   Nicotine    CRISPR
13m3f   Nicotine    CRISPR
13m4m   Nicotine    CRISPR
13m5f   Nicotine    CRISPR
19f4f   Nicotine    CRISPR
1f2f    Nicotine    CRISPR
1m2f    Nicotine    CRISPR
1m2m    Nicotine    CRISPR
3f4f    Nicotine    CRISPR
6f1m    Nicotine    CRISPR
6m3m    Nicotine    CRISPR
6m4f    Nicotine    CRISPR
7f4f    Nicotine    CRISPR
8f2f    Nicotine    CRISPR
8f3m    Nicotine    CRISPR

以下是清单:

genodata <- split(test, test$Line,drop=FALSE)
genodata
$`11m3m`
    Line Replicate Block     Group     Food Stop_Site Input Output
2  11m3m         1     1 NonCRISPR Nicotine         0    30     20
48 11m3m         1     3 NonCRISPR  Regular         0    30     20
49 11m3m         1     3 NonCRISPR Nicotine         0    30     16
74 11m3m         1     4 NonCRISPR  Regular         0    30     22
75 11m3m         2     4 NonCRISPR  Regular         0    30     22
76 11m3m         1     4 NonCRISPR Nicotine         0    30     26
77 11m3m         2     4 NonCRISPR Nicotine         0    30     25

$`13f2m`
    Line Replicate Block     Group     Food Stop_Site Input Output
4  13f2m         1     1 NonCRISPR Nicotine         0    30     20
30 13f2m         1     2 NonCRISPR  Regular         0    30     25
31 13f2m         1     2 NonCRISPR Nicotine         0    30     25
82 13f2m         1     4 NonCRISPR  Regular         0    30     27
83 13f2m         2     4 NonCRISPR  Regular         0    30     23
84 13f2m         1     4 NonCRISPR Nicotine         0    30     25
85 13f2m         2     4 NonCRISPR Nicotine         0    30     25

$`13f3f`
     Line Replicate Block  Group     Food Stop_Site Input Output
17  13f3f         1     1 CRISPR Nicotine         3    30     14
62  13f3f         1     3 CRISPR  Regular         3    30     19
63  13f3f         1     3 CRISPR Nicotine         3    30     13
142 13f3f         1     4 CRISPR  Regular         3    30     22
143 13f3f         2     4 CRISPR  Regular         3    30     26
144 13f3f         1     4 CRISPR Nicotine         3    30     12
145 13f3f         2     4 CRISPR Nicotine         3    30     16

$`13f5f`
    Line Replicate Block     Group     Food Stop_Site Input Output
6  13f5f         1     1 NonCRISPR Nicotine         0    30     17
26 13f5f         1     2 NonCRISPR  Regular         0    30     21
27 13f5f         1     2 NonCRISPR Nicotine         0    30     20
46 13f5f         1     3 NonCRISPR  Regular         0    30     24
47 13f5f         1     3 NonCRISPR Nicotine         0    30     20
90 13f5f         1     4 NonCRISPR  Regular         0    30     24
91 13f5f         2     4 NonCRISPR  Regular         0    30     25
92 13f5f         1     4 NonCRISPR Nicotine         0    30     25
93 13f5f         2     4 NonCRISPR Nicotine         0    30     25

$`13m2m`
     Line Replicate Block  Group     Food Stop_Site Input Output
15  13m2m         1     1 CRISPR Nicotine         2    30     14
38  13m2m         1     2 CRISPR  Regular         2    30     19
39  13m2m         1     2 CRISPR Nicotine         2    30      4
68  13m2m         1     3 CRISPR  Regular         2    30     17
69  13m2m         1     3 CRISPR Nicotine         2    30     11
124 13m2m         1     4 CRISPR  Regular         2    30     26
125 13m2m         2     4 CRISPR  Regular         2    30     22
126 13m2m         1     4 CRISPR Nicotine         2    30     11
127 13m2m         2     4 CRISPR Nicotine         2    30     15

$`13m3f`
     Line Replicate Block  Group     Food Stop_Site Input Output
13  13m3f         1     1 CRISPR Nicotine         3    30     15
136 13m3f         1     4 CRISPR  Regular         3    30     24
137 13m3f         2     4 CRISPR  Regular         3    30     27
138 13m3f         3     4 CRISPR  Regular         3    30     27
139 13m3f         1     4 CRISPR Nicotine         3    30     17
140 13m3f         2     4 CRISPR Nicotine         3    30     12
141 13m3f         3     4 CRISPR Nicotine         3    30     15

$`13m4m`
     Line Replicate Block  Group     Food Stop_Site Input Output
16  13m4m         1     1 CRISPR Nicotine         1    30     11
106 13m4m         1     4 CRISPR  Regular         1    30     29
107 13m4m         2     4 CRISPR  Regular         1    30     23
108 13m4m         3     4 CRISPR  Regular         1    30     22
109 13m4m         1     4 CRISPR Nicotine         1    30     17
110 13m4m         2     4 CRISPR Nicotine         1    30     17
111 13m4m         3     4 CRISPR Nicotine         1    30     18

$`13m5f`
     Line Replicate Block  Group     Food Stop_Site Input Output
22  13m5f         1     1 CRISPR Nicotine         3    30      3
40  13m5f         1     2 CRISPR  Regular         3    30     25
41  13m5f         1     2 CRISPR Nicotine         3    30     14
154 13m5f         1     4 CRISPR  Regular         3    30     28
155 13m5f         2     4 CRISPR  Regular         3    30     26
156 13m5f         1     4 CRISPR Nicotine         3    30     15
157 13m5f         2     4 CRISPR Nicotine         3    30     19

$`19f4f`
     Line Replicate Block  Group     Food Stop_Site Input Output
9   19f4f         1     1 CRISPR Nicotine         2    30      1
56  19f4f         1     3 CRISPR  Regular         2    30     20
57  19f4f         1     3 CRISPR Nicotine         2    30      8
112 19f4f         1     4 CRISPR  Regular         2    30     20
113 19f4f         2     4 CRISPR  Regular         2    30     21
114 19f4f         1     4 CRISPR Nicotine         2    30      6
115 19f4f         2     4 CRISPR Nicotine         2    30      8

$`1f1f`
   Line Replicate Block     Group     Food Stop_Site Input Output
3  1f1f         1     1 NonCRISPR Nicotine         0    30     19
50 1f1f         1     3 NonCRISPR  Regular         0    30     23
51 1f1f         1     3 NonCRISPR Nicotine         0    30     19
78 1f1f         1     4 NonCRISPR  Regular         0    30     23
79 1f1f         2     4 NonCRISPR  Regular         0    30     25
80 1f1f         1     4 NonCRISPR Nicotine         0    30     28
81 1f1f         2     4 NonCRISPR Nicotine         0    30     25

$`1f2f`
    Line Replicate Block  Group     Food Stop_Site Input Output
10  1f2f         1     1 CRISPR Nicotine         2    30      8
58  1f2f         1     3 CRISPR  Regular         2    30     21
59  1f2f         1     3 CRISPR Nicotine         2    30     13
116 1f2f         1     4 CRISPR  Regular         2    30     23
117 1f2f         2     4 CRISPR  Regular         2    30     25
118 1f2f         1     4 CRISPR Nicotine         2    30     15
119 1f2f         2     4 CRISPR Nicotine         2    30      8

$`1m2f`
    Line Replicate Block  Group     Food Stop_Site Input Output
11  1m2f         1     1 CRISPR Nicotine         2    30      6
60  1m2f         1     3 CRISPR  Regular         2    30     16
61  1m2f         1     3 CRISPR Nicotine         2    30      8
120 1m2f         1     4 CRISPR  Regular         2    30     19
121 1m2f         2     4 CRISPR  Regular         2    30     25
122 1m2f         1     4 CRISPR Nicotine         2    30     16
123 1m2f         2     4 CRISPR Nicotine         2    30     17

$`1m2m`
    Line Replicate Block  Group     Food Stop_Site Input Output
21  1m2m         1     1 CRISPR Nicotine         3    30     15
66  1m2m         1     3 CRISPR  Regular         3    30     20
67  1m2m         1     3 CRISPR Nicotine         3    30      9
150 1m2m         1     4 CRISPR  Regular         3    30     21
151 1m2m         2     4 CRISPR  Regular         3    30     24
152 1m2m         1     4 CRISPR Nicotine         3    30     10
153 1m2m         2     4 CRISPR Nicotine         3    30      9

$`2f4f`
   Line Replicate Block     Group     Food Stop_Site Input Output
7  2f4f         1     1 NonCRISPR Nicotine         0    30     18
24 2f4f         1     2 NonCRISPR  Regular         0    30     23
25 2f4f         1     2 NonCRISPR Nicotine         0    30     19
94 2f4f         1     4 NonCRISPR  Regular         0    30     22
95 2f4f         2     4 NonCRISPR  Regular         0    30     23
96 2f4f         1     4 NonCRISPR Nicotine         0    30     26
97 2f4f         2     4 NonCRISPR Nicotine         0    30     25

$`3f4f`
    Line Replicate Block  Group     Food Stop_Site Input Output
18  3f4f         1     1 CRISPR Nicotine         3    30      7
64  3f4f         1     3 CRISPR  Regular         3    30     23
65  3f4f         1     3 CRISPR Nicotine         3    30      5
146 3f4f         1     4 CRISPR  Regular         3    30     27
147 3f4f         2     4 CRISPR  Regular         3    30     25
148 3f4f         1     4 CRISPR Nicotine         3    30     11
149 3f4f         2     4 CRISPR Nicotine         3    30      6

$`3m5f`
   Line Replicate Block     Group     Food Stop_Site Input Output
1  3m5f         1     1 NonCRISPR Nicotine         0    30     21
44 3m5f         1     3 NonCRISPR  Regular         0    30     24
45 3m5f         1     3 NonCRISPR Nicotine         0    30     21
70 3m5f         1     4 NonCRISPR  Regular         0    30     19
71 3m5f         2     4 NonCRISPR  Regular         0    30     20
72 3m5f         1     4 NonCRISPR Nicotine         0    30     19
73 3m5f         2     4 NonCRISPR Nicotine         0    30     20

$`6f1m`
    Line Replicate Block  Group     Food Stop_Site Input Output
20  6f1m         1     1 CRISPR Nicotine         1    30      7
52  6f1m         1     3 CRISPR  Regular         1    30     21
53  6f1m         1     3 CRISPR Nicotine         1    30      4
102 6f1m         1     4 CRISPR  Regular         1    30     23
103 6f1m         2     4 CRISPR  Regular         1    30     26
104 6f1m         1     4 CRISPR Nicotine         1    30     10
105 6f1m         2     4 CRISPR Nicotine         1    30     10

$`6m3m`
    Line Replicate Block  Group     Food Stop_Site Input Output
14  6m3m         1     1 CRISPR Nicotine         1    30     17
42  6m3m         1     2 CRISPR  Regular         1    30     23
43  6m3m         1     2 CRISPR Nicotine         1    30     13
98  6m3m         1     4 CRISPR  Regular         1    30     24
99  6m3m         2     4 CRISPR  Regular         1    30     24
100 6m3m         1     4 CRISPR Nicotine         1    30     17
101 6m3m         2     4 CRISPR Nicotine         1    30     17

$`6m4f`
    Line Replicate Block  Group     Food Stop_Site Input Output
8   6m4f         1     1 CRISPR Nicotine         4    30     14
54  6m4f         1     3 CRISPR  Regular         4    30     16
55  6m4f         1     3 CRISPR Nicotine         4    30     13
162 6m4f         1     4 CRISPR  Regular         4    30     26
163 6m4f         2     4 CRISPR  Regular         4    30     24
164 6m4f         1     4 CRISPR Nicotine         4    30     11
165 6m4f         2     4 CRISPR Nicotine         4    30     10

$`7f4f`
    Line Replicate Block  Group     Food Stop_Site Input Output
23  7f4f         1     1 CRISPR Nicotine         3    30      4
36  7f4f         1     2 CRISPR  Regular         3    30     20
37  7f4f         1     2 CRISPR Nicotine         3    30      4
158 7f4f         1     4 CRISPR  Regular         3    30     21
159 7f4f         2     4 CRISPR  Regular         3    30     28
160 7f4f         1     4 CRISPR Nicotine         3    30     14
161 7f4f         2     4 CRISPR Nicotine         3    30     12

$`7f4m`
   Line Replicate Block     Group     Food Stop_Site Input Output
5  7f4m         1     1 NonCRISPR Nicotine         0    30     17
28 7f4m         1     2 NonCRISPR  Regular         0    30     23
29 7f4m         1     2 NonCRISPR Nicotine         0    30     19
86 7f4m         1     4 NonCRISPR  Regular         0    30     20
87 7f4m         2     4 NonCRISPR  Regular         0    30     27
88 7f4m         1     4 NonCRISPR Nicotine         0    30     22
89 7f4m         2     4 NonCRISPR Nicotine         0    30     24

$`8f2f`
    Line Replicate Block  Group     Food Stop_Site Input Output
19  8f2f         1     1 CRISPR Nicotine         2    30      7
32  8f2f         1     2 CRISPR  Regular         2    30     18
33  8f2f         1     2 CRISPR Nicotine         2    30      1
128 8f2f         1     4 CRISPR  Regular         2    30     20
129 8f2f         2     4 CRISPR  Regular         2    30     20
130 8f2f         1     4 CRISPR Nicotine         2    30      5
131 8f2f         2     4 CRISPR Nicotine         2    30      7

$`8f3m`
    Line Replicate Block  Group     Food Stop_Site Input Output
12  8f3m         1     1 CRISPR Nicotine         3    30     12
34  8f3m         1     2 CRISPR  Regular         3    30     27
35  8f3m         1     2 CRISPR Nicotine         3    30      5
132 8f3m         1     4 CRISPR  Regular         3    30     26
133 8f3m         2     4 CRISPR  Regular         3    30     24
134 8f3m         1     4 CRISPR Nicotine         3    30      9
135 8f3m         2     4 CRISPR Nicotine         3    30     14

预期输出

 $`11m3m`
        Line Replicate Block     Group     Food Stop_Site Input Output
    2  11m3m         1     1 NonCRISPR Nicotine         0    30     20
    48 11m3m         1     3 NonCRISPR  Regular         0    30     20
    49 11m3m         1     3 NonCRISPR Nicotine         0    30     16
    74 11m3m         1     4 NonCRISPR  Regular         0    30     22
    75 11m3m         2     4 NonCRISPR  Regular         0    30     22
    76 11m3m         1     4 NonCRISPR Nicotine         0    30     26
    77 11m3m         2     4 NonCRISPR Nicotine         0    30     25

    $`13f2m`
        Line Replicate Block     Group     Food Stop_Site Input Output
    4  13f2m         1     1 NonCRISPR Nicotine         0    30     20
    30 13f2m         1     2 NonCRISPR  Regular         0    30     25
    31 13f2m         1     2 NonCRISPR Nicotine         0    30     25
    82 13f2m         1     4 NonCRISPR  Regular         0    30     27
    83 13f2m         2     4 NonCRISPR  Regular         0    30     23
    84 13f2m         1     4 NonCRISPR Nicotine         0    30     25
    85 13f2m         2     4 NonCRISPR Nicotine         0    30     25

$`13f5f`
        Line Replicate Block     Group     Food Stop_Site Input Output
    6  13f5f         1     1 NonCRISPR Nicotine         0    30     17
    26 13f5f         1     2 NonCRISPR  Regular         0    30     21
    27 13f5f         1     2 NonCRISPR Nicotine         0    30     20
    46 13f5f         1     3 NonCRISPR  Regular         0    30     24
    47 13f5f         1     3 NonCRISPR Nicotine         0    30     20
    90 13f5f         1     4 NonCRISPR  Regular         0    30     24
    91 13f5f         2     4 NonCRISPR  Regular         0    30     25
    92 13f5f         1     4 NonCRISPR Nicotine         0    30     25
    93 13f5f         2     4 NonCRISPR Nicotine         0    30     25

$`1f1f`
       Line Replicate Block     Group     Food Stop_Site Input Output
    3  1f1f         1     1 NonCRISPR Nicotine         0    30     19
    50 1f1f         1     3 NonCRISPR  Regular         0    30     23
    51 1f1f         1     3 NonCRISPR Nicotine         0    30     19
    78 1f1f         1     4 NonCRISPR  Regular         0    30     23
    79 1f1f         2     4 NonCRISPR  Regular         0    30     25
    80 1f1f         1     4 NonCRISPR Nicotine         0    30     28
    81 1f1f         2     4 NonCRISPR Nicotine         0    30     25

1 个答案:

答案 0 :(得分:2)

以下内容应该有效,但我无法在没有样本数据的情况下进行验证:

# get the correct order
sortedData <- unique(GenoMEANs1$Line)

# apply order to list
genodata <- genodata[sortedData]

下面是一个玩具示例,显示这适用于奇怪命名的列表项(带有反引号的项目):

temp <- list(`13f2m`=1:10, `11m3m`=rnorm(10))
temp[sort(names(temp))]