[R] Order a data frame based on the order of another data frame

francesca casalino francy.casalino at gmail.com
Mon Mar 5 10:39:25 CET 2012


Hi, I am trying to match the order of the rownames of a dataframe with
the rownames of another dataframe (I can't simply sort both sets
because I would have to change the order of many other connected
datasets if I did that): Also, the second dataset (snp.matrix$fam) is
a snp matrix slot:

so for example:

data_one:
                                        x                    y                z
sample_1110001    -0.3352623     -1.141462    -0.4032494
sample_1110005     0.1862424      0.015944     0.1329059
sample_1110420     0.1309120   0.004005596    0.06117253
sample_2220017     0.1145205  -0.125090054    0.04957881

rownames(snp.matrix$fam)
 [1] "sample_2220017" "sample_1110420" "sample_1110001"
 [4] "sample_1110005"

I would like my data_one to look like this:
                               x                           y
            z
sample_2220017     0.1145205  -0.125090054    0.04957881
sample_1110420     0.1309120   0.004005596    0.06117253
sample_1110001    -0.3352623     -1.141462    -0.4032494
sample_1110005     0.1862424      0.015944     0.1329059


I have tried these but it doesn't work:
data_one[order(rownames(snp.matrix$fam)),]
data_one[rownames(data_oen)[order(rownames(snp.matrix$fam))],]

Thank you for your help!



More information about the R-help mailing list