lubridate - counting overlapping intervals for every interval

大城市里の小女人 提交于 2019-12-11 06:18:40

问题


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


回答1:


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))


来源:https://stackoverflow.com/questions/46240252/lubridate-counting-overlapping-intervals-for-every-interval

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!