If I have an example dataframe:
Date <- c("05/12/2012 05:17:00", "05/12/2012 06:10:00", "05/12/2012 06:12:00", "05/12/2012 06:14:00",
"06/12/2012 05:25:00", "06/12/2012 06:55:00", "06/12/2012 06:19:00", "06/12/2012 08:00:00",
"07/12/2012 05:00:00", "07/12/2012 05:19:00", "07/12/2012 06:04:00",
"07/12/2012 06:59:00")
Date <- strptime(Date, "%d/%m/%Y %H:%M")
a <- sample(12)
hour <- as.numeric(format(Date, "%H"))
min <- as.numeric(format(Date, "%M")) / 60
hours_mins <- hour + min
df1 <- data.frame(Date,a,hour, min, hours_mins, stringsAsFactors = FALSE)
I wish to be able to subset my dataframe leaving only data (on any day) between the hours of 05:15 and 06:15.
I converted the hours and mins to a decimal variable, and hoped I might be able to do something like:
df1[df1$hours_mins >= '5.25' & df1$hours_mins < '6.25']
... but alas this doesn't work. Does anyone have any suggestions?
Actually one of the advantages of the POSIXlt object is that it automatically carries critical date information.
Also if you want total hours (as a decimal) I added a column in the dataframe. I hope this will meet your needs.
Remove the quotes and add a comma in the end
df1[df1$hours_mins >= 5.25 & df1$hours_mins < 6.25,]