Search code examples
rtimecategorization

Is there any way to categorize the time (HH:MM)?


I need to write the meal timings according to day time. For example, between 05:00-10:59 will be categorized as "Breakfast", 11:00-16:59 as "Lunch", 17:00-22:59 as "Dinner" and 23:00-04:59 as "Night. I create the HH:MM data via "for loop" so the data was saved as character. When I used as.POSIXct or strptime function, it was converted again as 'date-time and tz'. I also couldn't use as.numeric or as.double functions. Because the data was like "23:21"

I want to categorize 235 (i in 14:248) different times as meal time. First of all I wrote "for loop" DATA_TIME's output is like that "19/12/2019 20:43"

Time <- character()
for (i in 14:248){
  DATE_TIME <- unlist(strsplit(lines[i], split= "\t"))[1]
  TIME <- unlist(strsplit(DATE_TIME, split=" "))[2]
  Time <- c(Time,TIME)
}

"Time" output is like that "20:43" I tried for that;

for (i in 14:248){
  DATE_TIME <- unlist(strsplit(lines[i], split= "\t"))[1]
  TIME <- unlist(strsplit(DATE_TIME, split=" "))[2]
  H_M <- as.POSIXct(TIME, format="%H:%M")
  h_m <- ifelse(H_M>='05:00' & H_M<'10:59', "Breakfast")
}
h_m
Time
if (Time>='05:00' & Time<'10:59'){
  print("Breakfast")
} else if (Time>='11:00' & Time<'16:59'){
  print("Lunch")
} else if (Time>='17:00' & Time<'22:59'){
  print("Lunch")
} else if (Time>='23:00' & Time<'04:59'){
  print("Night")
}

Solution

  • With package chron, coerce the vector of times to class "times" and findInterval will give an index into a vector of cut points.

    First, make up some data.

    set.seed(2023)
    x <- cbind(hour = sample(0:23, 10, TRUE), min = sample(0:59, 10, TRUE))
    x <- apply(x, 1, \(y) sprintf("%02d:%02d", y[1], y[2]))
    x
    #>  [1] "20:28" "15:48" "14:16" "08:04" "07:07" "18:33" "11:02" "01:14" "16:44"
    #> [10] "00:03"
    

    Created on 2023-03-30 with reprex v2.0.2

    Now the vector of cut points and their labels. And get the intervals where the data falls into.

    library(chron)
    
    cutpoint <- c("00:00:00", "5:00:00", "11:00:00", "17:00:00", "23:00:00")
    cutpoint <- times(cutpoint)
    cutpoint_names <- c("Night", "Breakfast", "Lunch", "Lunch", "Night")
    
    tt <- times(paste0(x, ":00"))
    i <- findInterval(tt, cutpoint)
    cutpoint_names[i]
    #>  [1] "Lunch"     "Lunch"     "Lunch"     "Breakfast" "Breakfast" "Lunch"    
    #>  [7] "Lunch"     "Night"     "Lunch"     "Night"
    

    Created on 2023-03-30 with reprex v2.0.2