You can sort one column of characters in one dataframe by the order of another column in another dataframe in R by using the match()
function along with order()
function. Below is an example code snippet that demonstrates how to achieve this:
371 chars13 lines
In this code snippet, we first define two dataframes df1
and df2
. We then sort df1
by the order of the name
column in df2
using the match()
function to get the corresponding order index and order()
function to sort based on that index. The resulting sorted df1
is stored in df1_sorted
.
Ensure that the names in the name
column of df1
and df2
match exactly for this method to work effectively.
gistlibby LogSnag