R:带有POSIXct日期的剪切功能:根据一天中的时间创建特定类别 [英] R: Cut function with POSIXct dates: Creating specific categories based on time of day

查看:77
本文介绍了R:带有POSIXct日期的剪切功能:根据一天中的时间创建特定类别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要开始分析一些训练有素的数据。数据集由医院程序代码,所述程序的值和日期/时间戳组成。我正在尝试将时间戳分为一天中的时间类别,因为某些程序值会根据早晨/晚上而有所不同。

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.

我尝试使用 cut(),但是我似乎不知道如何输入特定时间类别的时间间隔(例如00:00到06:00 =斋戒,06:01- 10:00 =早晨用餐,...)。

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.

推荐答案

您可以使用包中的 hour 提取日期中的小时

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]> 

这篇关于R:带有POSIXct日期的剪切功能:根据一天中的时间创建特定类别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆