I've produced a sample dataset to illustrate my solution: Using lubridate
we can convert the colnames to a time-format and then order the data.frame
accordingly:
df <- structure(list(`0:00:00` = 1:9, `1:00:00` = 10:18, `10:00:00` = 19:27,
`11:00:00` = 28:36, `12:00:00` = 37:45, `13:00:00` = 46:54,
`14:00:00` = 55:63), .Names = c("0:00:00", "1:00:00", "10:00:00",
"11:00:00", "12:00:00", "13:00:00", "14:00:00"), class = "data.frame", row.names = c(NA,
-9L))
df2 <- structure(list(`0:00:00` = 1:9, `1:00:00` = 19:27, `10:00:00` = 28:36,
`11:00:00` = 10:18, `12:00:00` = 37:45, `13:00:00` = 46:54,
`14:00:00` = 55:63), .Names = c("0:00:00", "1:00:00", "10:00:00",
"11:00:00", "12:00:00", "13:00:00", "14:00:00"), class = "data.frame", row.names = c(NA,
-9L))
library(lubridate)
df <- df[,order(hms(names(df)))]
df2 <- df2[,order(hms(names(df2)))]