Order a data frame according to a list in R

Viewed 46

I have a dataframe 'df'

df


sel num    y
 a   1   cdv
 b   2   gfv
 c   3   dfv
 d   4   fdv
 e   5   hyf
 a   6   6hy
 b   7   rgb
 c   8   tgbh
 d   9   tgy
 e  10   hytg

I need to arrange the dataframe based on the column 'sel' and also by increasing or decreasing order of the numeric column 'num'. the order is to be predefined in a list or a dataframe like,

df_order = c('c','a','e','d','b')

So the output should be like.

df1
    sel num    y
1    c   3  dfv
2    c   8 tgbh
3    a   1  cdv
4    a   6  6hy
5    e   5  hyf
6    e  10 hytg
7    d   4  fdv
8    d   9  tgy
9    b   2  gfv
10   b   7  rgb

Is there any way to order my dataframe based on a particular column that needs to be arranged based on a list?

0 Answers
Related