R: Cut function with POSIXct dates: Creating specific categories based on time of day

独自空忆成欢 提交于 2019-12-12 02:16:15

问题


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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!