I am working with two dataframes. I want to reorder the rows of one dataframe so that it is identical to the order of the rows in the second dataframe.
I have one dataframe with counts and the other with metadata. I need the same order to generate vectors of metadata for statistical comparisons.
df1
ColName1 ColName2 ColName3
RowName1 3 0 5
Rowname2 0 1 7
Rowname3 0 2 2
Rowname4 9 3 4
df2
ColName1 ColName2
RowName3 A Q
Rowname2 A W
Rowname4 B Q
Rowname1 B W
I expect to get
df2
ColName1 ColName2
RowName1 B W
Rowname2 A W
Rowname3 A Q
Rowname4 B Q
I have seen post no. 27362718 and tried the below, but it hasn't worked.
df2[order(match(rownames(df2), rownames(df1))), ]
This lists order indicating which rows does not match:
match(rownames(df2), rownames(df1))
This lists order but the rest does not set the order:
order(match(rownames(df2), rownames(df1)))