Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sort a dataframe by datetime for every factor level in other column

I have the dataframe below and I would like to sort it by the datetime column Run Date (from earliest to latest dates) but for each specific factor of the column SN which in this case has 2 levels 134080504 and T133230503A.

dat<-structure(list(SN = c("134080504", "134080504", "134080504", 
"134080504", "134080504", "134080504", "134080504", "134080504", 
"134080504", "134080504", "134080504", "134080504", "134080504", 
"134080504", "134080504", "134080504", "134080504", "134080504", 
"134080504", "134080504", "134080504", "134080504", "134080504", 
"134080504", "134080504", "134080504", "134080504", "T133230503A", 
"T133230503A", "T133230503A", "T133230503A", "T133230503A", "T133230503A", 
"T133230503A", "T133230503A", "T133230503A", "T133230503A", "T133230503A", 
"T133230503A", "T133230503A", "T133230503A", "T133230503A", "T133230503A", 
"T133230503A", "T133230503A", "T133230503A", "T133230503A", "T133230503A", 
"T133230503A", "T133230503A"), `Status [F/S]` = c("S", "S", "S", 
"S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", 
"S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", 
"S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", "S", 
"S", "S", "S", "S", "S", "S", "S", "S"), `Run Date` = structure(c(1256466600, 
1256264100, 1256228100, 1256219100, 1256202000, 1228897800, 1218629760, 
1177851660, 1177462800, 1172398500, 1171795800, 1164396600, 1163971800, 
1157567400, 1157224500, 1157079000, 1156874100, 1156752000, 1156410000, 
1148266800, 1147984200, 1147780800, 1147177800, 1142753400, 1141551000, 
1141372800, 1130468400, 1625724600, 1625724600, 1625724600, 1623628800, 
1623628800, 1623628800, 1623358800, 1623358800, 1623358800, 1566812460, 
1566812460, 1566812460, 1566665700, 1566665700, 1566665700, 1554503520, 
1554503520, 1554503520, 1554161880, 1554161880, 1554161880, 1553929200,
1553929200), class = c("POSIXct", "POSIXt"), tzone = "UTC")), row.names = c(NA, 
50L), class = c("tbl_df", "tbl", "data.frame"))

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

Try it!

library(tidyverse)
dat %>% arrange(`Run Date`, SN)
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading