简体   繁体   中英

reorder date columns in r

Hi I have the following data.frame

    variable   name    new_skill    01/05/2015   01/08/2014   02/01/2015   02/05/2014

    task1      H&I     A             3.76           4.0         3.5        4.5
    task2      H&I    Broadband      3.12           3.30        4.5        5.5

There is 2 years worth of weekly dates in the columns above...

How to reorder them?

I tried something like data.frame[,order()]

this didn't work.

thanks

Try this,

df[, order(as.Date(colnames(df), format = "%d/%m/%Y"))]

This would sort the date columns according to the date and rest all columns would append to the end according to their occurrence in the original data frame.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM