Rüdiger Kladt
Rüdiger Kladt

Reputation: 119

lubridate - counting overlapping intervals for every interval

i am not very experienced in programmming today, but did some work in the past far away.

We support shared cars and for every car there are bookings with a start-datetime and an end-datetime. Start-dt an end-dt for every booking is at full 00 or 30 minutes and has a duration >= 30 Minutes.

Now we have many cars at the same place an i want to see, how many cars had a booking at overlapping times.

For this i build a sequence of timeslots with a duration of 30 minutes between two times.

library(dplyr)
TimeSlot =
   tibble(seq(
     from = as.POSIXlt("2013-07-01"),
     to = as.POSIXlt("2013-12-01"),
     1800 ))
 TimeSlot <- cbind(TimeSlot, c(0L))
 colnames(TimeSlot) <- c("Slot", "count")
 TimeSlot$count <- as.integer(TimeSlot$count)

Then for every timeslot i count the bookings, which overlap that timeslot. This code works:

for(j in 1:length(TimeSlot$count))
{
   for (i in 1:length(bookings$start)) {
     if ((TimeSlot[j, "Slot"] >= bookings[i, "start"]) &&
         (TimeSlot[j, "Slot"] < bookings[i, "end"])) {
       TimeSlot[j, "count"] = TimeSlot[j, "count"] + 1
       # rk_j = j
     }
   }
 }

and i get a result.

This takes a while an i think, that is not very r-like. Now, before i start to optimize this code, i will ask the community of more experienced people, if there is an r-like way to solve my problem.

Best regards Ruediger

Upvotes: 2

Views: 1469

Answers (1)

pogibas
pogibas

Reputation: 28379

Without knowing how bookings look like it's not that easy, but this logic should work. As you tagged question with lubridate I posted solution with it.

library(lubridate)

# Transform time for Slot using lubridate
TimeSlot$Slot <- ymd_hms(TimeSlot$Slot)

# Create example dataset for bookings
bookings <- data.frame(start = c(TimeSlot$Slot[4], TimeSlot$Slot[12]), 
                       end   = c(TimeSlot$Slot[10], TimeSlot$Slot[22]))
# Transform booking to time interval
bookingsInterval <- interval(bookings$start, bookings$end)

# For each time slot sum how many overlaps with bookings interval
TimeSlot$count <- sapply(TimeSlot$Slot, function(x) sum(x %within% bookingsInterval))

Upvotes: 1

Related Questions