To order a dataframe by the order of a column in another dataframe in R, you can use the match function to match the values in the column of the first dataframe with the values in the column of the second dataframe, and then use the resulting index to order the first dataframe.
Here is an example:
main.r342 chars17 lines
This will output:
main.r66 chars7 lines
The match function returns the index of the first occurrence of each value in df1$id in df2$id. The order function then uses these indices to order the rows of df1.
Alternatively, you can use the dplyr package and the left_join function to achieve the same result:
main.r147 chars9 lines
Note that this assumes that there are no duplicate values in the id column of df2. If there are duplicates, the match function will return the index of the first occurrence of each value, which may not be what you want. In that case, you may need to use a different approach, such as using the merge function to create a new dataframe with the combined data.
gistlibby LogSnag