Search code examples
rdatetimedplyrposixct

Find if a time exists between two different times, type issue


I have a dataframe as so

df <- structure(list(TIME = c("11:15:00", NA, "15:15:00", "12:00:00", 
"18:40:00", "18:15:00", "7:10:00", "15:58:00", "10:00:00", "10:00:00"
)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", "data.frame"
))

And I basically want to create a new variable which tells me if the time is in a certain group.

I wrote the following but it's not correct, tried changing to as.POSICxt but no dice.

df <- df %>% 
  mutate(time_groups  = ifelse(between(as.POSIXct(TIME),00:00, 5:59), 1,
                        ifelse(between(as.POSIXct(TIME),06:00, 8:59), 2,
                        ifelse(between(as.POSIXct(TIME),09:00,11:59), 3,
                        ifelse(between(as.POSIXct(TIME),12:00,14:59), 4,
                        ifelse(between(as.POSIXct(TIME),15:00,17:59), 5,
                        ifelse(between(as.POSIXct(TIME),18:00,23:59), 6,
), NA)

Solution

  • You could use the findInterval function:

    library(tidyverse)
    library(lubridate)
    
    a <- c("00:00","5:59", "8:59", "11:59", "14:59", "17:59", "23:59")
    b <- ymd_hm(paste(Sys.Date(), a))
    
    df %>% 
      mutate(Interval = findInterval(ymd_hms(paste(Sys.Date(), TIME)), b))
    
       TIME     Interval
      <chr>       <int>
     1 11:15:00        3
     2 NA             NA
     3 15:15:00        5
     4 12:00:00        4
     5 18:40:00        6
     6 18:15:00        6
     7 7:10:00         2
     8 15:58:00        5
     9 10:00:00        3
    10 10:00:00        3