问题
I have a small training set of data that I am beginning to analyze. The dataset is comprised of hospital procedural codes, values for said procedure, and a date/time stamp. I am attempting to break down the time stamps into 'time of day' categories, as some of the procedural values will vary depending on morning/evening.
I have tried using cut()
, but I can't seem to figure out how to input specific time intervals for the categories (ex. 00:00 to 06:00 = "Fasting", 06:01- 10:00 = "Morning Meal", ...).
Any help or direction would be greatly appreciated.
回答1:
You can extract the hour from the date using hour
in package lubridate.
> library(lubridate)
> now()
[1] "2016-04-26 23:21:56 CEST"
> dt <- now()
> hour(dt)
[1] 23
> cut(hour(dt), breaks = c(0, 6, 12, 18, 24), include.lowest = TRUE)
[1] (18,24]
Levels: [0,6] (6,12] (12,18] (18,24]>
来源:https://stackoverflow.com/questions/36875223/r-cut-function-with-posixct-dates-creating-specific-categories-based-on-time-o