How to sort a data frame by date

前端 未结 7 831
孤城傲影
孤城傲影 2020-11-28 21:46

I need to sort a data frame by date in R. The dates are all in the form of \"dd/mm/yyyy\". The dates are in the 3rd column. The column header is V3. I have seen how to s

7条回答
  •  予麋鹿
    予麋鹿 (楼主)
    2020-11-28 22:01

    In case you want to sort dates with descending order the minus sign doesn't work with Dates.

    out <- DF[rev(order(as.Date(DF$end))),]
    

    However you can have the same effect with a general purpose function: rev(). Therefore, you mix rev and order like:

    #init data
    DF <- data.frame(ID=c('ID3', 'ID2','ID1'), end=c('4/1/09 12:00', '6/1/10 14:20', '1/1/11 11:10')
    #change order
    out <- DF[rev(order(as.Date(DF$end))),]
    

    Hope it helped.

提交回复
热议问题