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")
}
With package
chron, coerce the vector of times to class"times"andfindIntervalwill give an index into a vector of cut points.First, make up some data.
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.
Created on 2023-03-30 with reprex v2.0.2