Given a data set like below. I would like to count how many times a particular hour of the day (00:00, 01:00, ...., 22:00, 23:00) falls completely within any of the given intervals.
The date of occurrence doesn't matter. Just the overall count.
### This code is to create a data set similar to the one I am using.
### This is a function I found on here to generate random times
latemail <- function(N, st="2012/01/01", et="2012/12/31") {
st <- as.POSIXct(as.Date(st))
et <- as.POSIXct(as.Date(et))
dt <- as.numeric(difftime(et,st,unit="sec"))
ev <- sort(runif(N, 0, dt))
rt <- st + ev
}
set.seed(123)
startTimes <- latemail(5)
endTimes <- startTimes +18000
my_data <- data.frame(startTimes, endTimes)
> my_data
start end
1 2012-04-14 16:10:44 2012-04-14 21:10:44
2 2012-05-28 23:38:16 2012-05-29 04:38:16
3 2012-10-14 10:33:10 2012-10-14 15:33:10
4 2012-11-17 23:13:56 2012-11-18 04:13:56
5 2012-12-08 22:29:36 2012-12-09 03:29:36
So that hopefully helps give you an idea of what I am working with.
Ideally the output would be a dataset with one variable for the hour, and another for the count of occurrences. Like this
hour count
1 00:00 3
2 01:00 3
3 etc ?
How to doing this in different increments (say 15 minutes) would also be great to know.
Thank you!