简体   繁体   中英

R-sort time by Month,day,Year and then time which is in AM/PM and also convert the time to 24hr format

here is a table in RI have

stopdate                             
8/14/2015  11:38:15AM    
8/14/2015  11:47:59AM    
8/14/2015  12:04:36PM    
8/14/2015   3:08:04PM    
8/14/2015   3:30:25PM    
8/14/2015  10:30:26PM    
8/1/2015   11:19:31AM    
8/15/2015   1:49:14PM

I would like to sort it by month/day/year and then by time, however its all under one column.

so how can I convert the time to 24 hr format, and then sort it by month/day/year and then by time?

Thank you

Install package lubridate . Then, assuming your data frame is named df1 , you can convert the dates and times and put them in a new column like this:

library(lubridate)
df1$datetime <- mdy_hms(df1$stopdate)

You can sort (earliest to latest) using:

df1 <- df1[order(df1$datetime), ]

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