如何合并这两个data.frame并保持正确的顺序?

时间:2016-07-20 22:26:22

标签: r dataframe merge

我有一个看起来像这样的data.frame:

          gvs order           labels color
1  -2.3321916     1           Adygei     1
13 -0.8519079     2           Basque     1
46 -0.9298674     3           French     1
47 -2.8859587     4          Italian     1
2  -1.4996229     5         Orcadian     1
9  -1.5597359     6          Russian     1
48 -1.4494841     7        Sardinian     1
14 -2.4279528     8           Tuscan     1
15 -3.1717421     9          Bedouin     2
22 -0.5058627    10            Druze     2
39 -2.6491331    11         Mozabite     2
23 -0.7819299    12      Palestinian     2
24 -1.4095947    13          Balochi     3
10 -1.2534511    14           Brahui     3
3   1.7958170    15          Burusho     3
25  2.2810477    16           Hazara     3
16 -0.9258497    17           Kalash     3
26 -0.9007551    18          Makrani     3
4   2.5543214    19           Pathan     3
27  2.6614486    20           Sindhi     3
17 -1.2207974    21           Uygurf     3
40  2.3706977    22        Cambodian     4
28 -0.9441980    23              Dai     4
18 -1.0325107    24             Daur     4
49 -0.7381369    25              Han     4
41 -2.7590587    26           Hezhen     4
50 -0.5644325    27         Japanese     4
44 -0.8449225    28             Lahu     4
29 -0.7237586    29             Miao     4
30 -0.9452944    30          Mongola     4
11 -0.1625003    31             Naxi     4
31 -1.2035258    32           Oroqen     4
5  -2.7758460    33              She     4
32 -0.7703779    34               Tu     4
12 -1.0265275    35            Tujia     4
45 -1.1163019    36             Xibo     4
19 -3.2102686    37            Yakut     4
42 -0.9614190    38               Yi     4
6  -1.9659984    39        Colombian     5
51 -0.9195156    40        Karitiana     5
7   2.1239768    41             Maya     5
33 -3.0895998    42             Pima     5
20 -0.9377928    43            Surui     5
43 -1.6961014    44       Melanesian     6
34 -0.7037952    45           Papuan     6
35 -1.9311354    46       BantuKenya     7
8  -1.8515908    47 BantuSouthAfrica     7
21 -1.7657017    48       BiakaPygmy     7
36 -0.5423822    49         Mandenka     7
37 -1.6244801    50       MbutiPygmy     7
38 -0.9049735    51              San     7
52  2.0949378    52           Yoruba     7

我有另一个data.frame2,如下所示:

                           labels pvals
Adygei                     Adygei 0.914
Balochi                   Balochi 0.158
BantuKenya             BantuKenya 0.484
BantuSouthAfrica BantuSouthAfrica 0.016
Basque                     Basque 0.218
Bedouin                   Bedouin 0.914
BiakaPygmy             BiakaPygmy 0.538
Brahui                     Brahui 0.162
Burusho                   Burusho 0.414
Cambodian               Cambodian 0.118
Colombian               Colombian 0.166
Dai                           Dai 0.686
Daur                         Daur 0.932
Druze                       Druze 0.220
French                     French 0.000
Han                           Han 0.794
Hazara                     Hazara 0.152
Hezhen                     Hezhen 0.182
Italian                   Italian 0.024
Japanese                 Japanese 0.366
Kalash                     Kalash 0.974
Karitiana               Karitiana 0.660
Lahu                         Lahu 0.560
Makrani                   Makrani 0.226
Mandenka                 Mandenka 0.076
Maya                         Maya 0.818
MbutiPygmy             MbutiPygmy 0.054
Melanesian             Melanesian 0.414
Miao                         Miao 0.194
Mongola                   Mongola 0.768
Mozabite                 Mozabite 0.200
Naxi                         Naxi 0.554
Orcadian                 Orcadian 0.148
Oroqen                     Oroqen 0.782
Palestinian           Palestinian 0.552
Papuan                     Papuan 0.386
Pathan                     Pathan 0.112
Pima                         Pima 0.818
Russian                   Russian 0.626
San                           San 0.478
Sardinian               Sardinian 0.516
She                           She 0.912
Sindhi                     Sindhi 0.338
Surui                       Surui 0.536
Tu                             Tu 0.254
Tujia                       Tujia 0.912
Tuscan                     Tuscan 0.420
Uygur                       Uygur 0.652
Xibo                         Xibo 0.292
Yakut                       Yakut 0.030
Yi                             Yi 0.838
Yoruba                     Yoruba 0.904

我想在原始data.frame中添加pvals列,添加与正确填充对应的pval,而不更改输出的顺序。 IE,我希望我的输出仍然有标签顺序:Adygei,巴斯克语,法语,意大利语......等等。有谁知道怎么做?我已经尝试了merge(),但它似乎按字母顺序重新组织了labels

2 个答案:

答案 0 :(得分:1)

另一种方式(使用plyr包的连接)

col s5 m5 l5

答案 1 :(得分:0)

您可以直接使用match

df1$pvals <- df2$pvals[match(df1$labels, df2$labels)]