R: reorder data after split() command using unconventional date column

157 Views Asked by At

For easier data manipulation purposes, I have a data frame in R that contains a column with date and hour values written as such: june 5 23. "june 5" is the month (no year included), and "23" is the hour (including minutes and seconds would cause issues later in my data manipulation.) I successfully split this large data frame into several smaller data frames by date and hour, using this command:

dat<-split(df,with(df, df_datehour),drop=FALSE)

However, split, as expected, ordered my data lexicographically and so it was organized as april --> june --> may instead of april --> may --> june. I tried to deal with this problem by using the fix I found online, which was to convert my date column using as.Date(). It doesn't seem to be working, though. I'm using the following line of code to convert to Date:

dat<-split(df,as.Date(with(df, df_datehour),format="%B %d %H"),drop=FALSE)

This line of code does manage to put the data in order by month and day, but it drops the hour completely, as you can see from this snippet of my results:

labels(dat)
[57] "2015-06-05"

Does anyone know of a fix for this problem?

1

There are 1 best solutions below

0
On BEST ANSWER

Convert the names to time format, reorder and reorder list by index.

dat[order(strptime(names(dat), "%B %d %H"))]