Here is one way. Given the comments of the OP, it seems that using cut
is a good approach here. Since there is no reproducible example, I created a small sample to demonstrate the function. Since you have a large data set, I think you want to update your R and use the data.table
package. If you stick to old versions of R, the transform
approach would be your choice.
# Create a sample data
mydf <- data.frame(id = 1:7,
time = c("01:00:00", "05:30:00", "10:00:00",
"14:00:00", "17:00:00", "20:00:00", "23:00:00"),
stringsAsFactors = FALSE)
# id time
#1 1 01:00:00
#2 2 05:30:00
#3 3 10:00:00
#4 4 14:00:00
#5 5 17:00:00
#6 6 20:00:00
#7 7 23:00:00
library(chron)
library(dplyr)
library(data.table)
# Convert character to times
mydf$time <- times(mydf$time)
# Base R approach
transform(mydf,
day_period = cut(time,
breaks = times(c("00:00:00", "05:00:00", "09:00:00",
"13:00:00", "17:00:00", "21:00:00", "23:59:00")),
labels = c("Late night", "Early morning", "Late morning",
"Early afternoon", "Late afternoon", "Evening")))
# dplyr approach
mutate(mydf,
day_period = cut(time,
breaks = times(c("00:00:00", "05:00:00", "09:00:00",
"13:00:00", "17:00:00", "21:00:00", "23:59:00")),
labels = c("Late night", "Early morning", "Late morning",
"Early afternoon", "Late afternoon", "Evening")))
# data.table approach
setDT(mydf)[, day_period := cut(time,
breaks = times(c("00:00:00", "05:00:00", "09:00:00",
"13:00:00", "17:00:00", "21:00:00",
"23:59:00")),
labels = c("Late night", "Early morning", "Late morning",
"Early afternoon", "Late afternoon", "Evening"))][]
# id time day_period
#1: 1 01:00:00 Late night
#2: 2 05:30:00 Early morning
#3: 3 10:00:00 Late morning
#4: 4 14:00:00 Early afternoon
#5: 5 17:00:00 Early afternoon
#6: 6 20:00:00 Late afternoon
#7: 7 23:00:00 Evening