Reorder factor levels by day of the week in R

前端 未结 2 1780
长发绾君心
长发绾君心 2021-02-01 20:16

I have the following data.frame in R:

> daily
        DoW         Duration
1    Friday 14.0000000000000
2    Monday 21.0000000000000
3  Saturday 12.0000000000         


        
2条回答
  •  挽巷
    挽巷 (楼主)
    2021-02-01 21:23

    You need to specify the levels in factor and then use order with indexing:

    daily$DoW <- factor(daily$DoW, levels= c("Sunday", "Monday", 
        "Tuesday", "Wednesday", "Thursday", "Friday", "Saturday"))
    
    daily[order(daily$DoW), ]
    

提交回复
热议问题