0

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?

  • So if I am understanding correctly, you are not happy that the names of your list are not in the right order? You can do `dat[c("april", "may", "june", etc)]` to redefine the order. – Vlo Jun 23 '15 at 14:29
  • The problem is that I would like to redefine the order and include the day of the month and the hour of the day, as well. I believe (correct me if I'm wrong) that if I used your method, I would have to do dat[c("april 1 1", "april 1 2", "april 1 3", etc)] which would take a very long time, as I have many data points. I'm trying to figure out if there's a quicker solution. – hot_tamales Jun 23 '15 at 14:51
  • 1
    `dat[order(strptime(names(dat), "%B %d %H"))]` will convert the names to a time format, sort the names, then return the index for reordering. Perhaps most importantly, I don't understand the need for your names to be in order in a list. – Vlo Jun 23 '15 at 15:04
  • Good point, thank you. I think I just realized that order wasn't exactly necessary to my actual data analysis and I really only need it for graphing later on. At that point, I ought to be able to reorder it more easily. Thanks for the help! – hot_tamales Jun 23 '15 at 15:37

1 Answers1

0

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

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

Vlo
  • 3,168
  • 13
  • 27