How to order a data.frame based on row.names in another data frame?

时间:2016-04-04 18:46:44

标签: r sorting dataframe

Basically, I had an initial data.frame where I used participants name as row.names. For various reasons, I had to stack then merge the data in the long format and then based on a factor variable I selected only a proportion of that data. And then I aggregated this so I have the wide data again with participants as row.names. However, the order is not the same. so if I wanted to cbind the original wide data with the new data it would mess up my data as the order of row.names is different. I tried sort(), order(), transform() and read several questions, but I couldn't find a way of doing it.

DF1
>               V1           V2           V3
>     AAA        24           22           37
>     BBB        21           22           33
>     CCC        30           32           38
>     DDD        21           23           35

The other data frame has the same dim(), however different order of the row.names.

DF2 row.names
>      BBB
>      CCC
>      AAA
>      DDD

I would like to sort DF1 based of rownames(DF2) so that the variables stay with the rownames, so I can then cbind (D1, D2) and because the rownames are equal, the same variable would belong to the same participant. I might have overcomplicated it, sorry :)

Basically, I want to reorder the whole data.frame(DF1) based on rownames in DF2.

I know it's a lame question, but I couldn't find a working answer.

2 个答案:

答案 0 :(得分:7)

Here's one option, using the built-in mtcars data frame for illustration:

# Create new sorted data frame. This is analogous to the second data frame
# by which you want to sort the first one.
mtcars.sorted = mtcars[order(rownames(mtcars)),]

# Sort original data frame by the order of the new data frame
mtcars[match(rownames(mtcars.sorted), rownames(mtcars)),]

So, in your case, the code would be:

DF1[match(rownames(DF2), rownames(DF1)), ]

答案 1 :(得分:3)

You can use match

> df <- data.frame(a=c("AAA","BBB","CCC"),b=1:3,c=6:8,d=9:11)
> df
   a  b c  d
1 AAA 1 6  9
2 BBB 2 7 10
3 CCC 3 8 11

> df1 <- data.frame(a=c("CCC","AAA","BBB"))
> df1
   a
1 CCC
2 AAA
3 BBB

> Final_df <- df[match(df1$a,df$a),]   ## Here it is
> Final_df 
   a  b c  d
3 CCC 3 8 11
1 AAA 1 6  9
2 BBB 2 7 10