I have this vector in a data frame of times in the format of hours:minutes that I want converted to categorical times of day:
time <- c(\"15:03\", \"0
Using some regex and ifelse
regex
ifelse
df$hour <- as.numeric(gsub("\\:.*$", "", df$time)) df$cat <- with(df, ifelse(hour >= 5 & hour<=11, "morning", ifelse(hour>11 & hour<=16, "evening", "night"))) df time hour cat 1 15:03 15 evening 2 08:01 8 morning 3 11:59 11 morning 4 23:47 23 night 5 14:20 14 evening