user1172558
user1172558

Reputation: 655

Sort a data.frame using row names of another data.frame

I need to sort the following data.frame (table 1):

X Y
A 1
B 5
C 0
D 3

based on the results of another data.frame (table 2):

X Y
C 10
B 9
A 8
D 7

So, data.frame # 1 ends like this:

X Y
C 0
B 5
A 1
D 3

How do I do this? I've tried to use:

table1[order(row names(table1),]

But I get the following error:

Subscript out of bound.

Upvotes: 1

Views: 265

Answers (1)

Jaap
Jaap

Reputation: 83215

This should give the desired result:

table1[order(table2$X),]

Upvotes: 2

Related Questions