Search code examples
rdataframetimedata-cleaninglubridate

Calculate time within time interval between start and end time and create time indicators


I have a data frame with the start and end time, as shown in the following.

> timedf <- data.frame(startime = c("0105","0205","0230","0245","0245"),
+                      endtime  = c("0130", "0330","0330", "0400", "0405"))
> 
> timedf
  startime endtime
1     0105    0130
2     0205    0330
3     0230    0330
4     0245    0400
5     0245    0405

I want to create 24 hourly indicators to show whether the time interval between start and end time falls in specific hour windows (as measured by hour indicator). For example, what I want is

  startime endtime time_0am time_1am time_2am time_3am time_4am time_5am time_6am
1     0105    0130        0        1        0        0        0        0        0
2     0205    0330        0        0        1        1        0        0        0
3     0230    0330        0        0        1        1        0        0        0
4     0245    0400        0        0        1        1        0        0        0
5     0245    0405        0        0        1        1        1        0        0

How could I do this in R?

Thanks!


Solution

  • Here a base R solution As Ottie comments, you need to specify wether intervals are open or closed. This way consideres only hours (not minutes), take both extremes in the interval, i think it replicates the example in your question.

    
    cols <- sapply(0:23, function(i) {
      as.integer(
        i >= sapply(strptime(timedf$startime, "%H%M"), function(j) j$hour) &
        i <= sapply(strptime(timedf$endtime, "%H%M"),function(j) j$hour))})
    
    colnames(cols) <- c(paste0("time_", 0:11, "am"), paste0("time_", c(12,1:11), "pm")) 
    
    timedf <- cbind(timedf, cols)