I have a data frame with start dates and end dates, along with the number of people registered for an event. I would like to calculate the number of hours each party is present for within a specific timeframe (e.g., 07:00 – 17:00)
If I use the following example data.frame…
d <- data.frame(startDate = c(as.POSIXct("2011-06-04 08:00:00"), as.POSIXct("2011-06-03 08:00:00"),
as.POSIXct("2011-09-12 10:00:00")),
endDate = c(as.POSIXct("2011-06-06 11:00:00"), as.POSIXct("2011-06-04 11:00:00"),
as.POSIXct("2011-09-12 18:00:00")),
partysize = c(124,442,323))
open <- "07:00"
close <- "17:00"
I would like my result set to look something like this:
day numhours partysize
2011-06-04 9 124
2011-06-05 10 124
2011-06-06 4 124
2011-06-03 9 442
2011-06-04 4 442
2011-09-12 7 323
note: numhours is the number of hours the date was included between the open and close times
Thanks in advance,
–JT
Sorry its very messy and I used 7 and 17 instead of your open and close
Basically we identify how many days each party size stays for. For a given day we find the applicable open and close. Then we subtract open from close. The dataframe is eventually formed but it could probably have been created in the res<- step…..