参考POSIXct格式时间区分高峰和非高峰时段
Distinguish Peak and Off-Peak traffic periods referring to POSIXct format time
这里我有一些在伦敦收集的每分钟空气污染浓度数据。这种污染物是一种与交通有关的空气污染物。我想添加另一个带有因子变量的列(Peak;Off-Peak)以区分在 期间收集的数据高峰时间(上午6:30-9:30)或非高峰时间(16:00至19:00)仅限周一至周五。当前时间列为 POSIXct 格式 date+time。
Sample Data
问题 1:我是否应该执行以下操作:
1.remove周六和周日数据
2.extract time 从 date+time
3.identify 时间范围 (6:30 am-9:30 am) 和 (16:00 到 19:00)
或者有一种方法可以使用 date+time (POSIXct)
来识别一天的时间范围
QUESTION 2:How 我可以从 POSIXct 格式的 date+time 变量中正确提取 time ,可用于时间范围((6:30 am-9:30 am) and (16:00 to 19:00)) identification?
您可以使用 chron 包,因为它具有确定工作日的功能。它还具有假期功能。 lubridate 包可用于从 date/time 变量中提取小时和分钟。
library(chron)
library(lubridate)
data %>%
mutate(peak = case_when(
is.weekend(as.Date(date)) ~ FALSE,
(hour(date)==6 & minute(date)>=30 | hour(date)>6) &
(hour(date)==9 & minute(date)<=30 | hour(date)<9) ~ TRUE,
TRUE ~ FALSE),
offpeak = case_when(
is.weekend(as.Date(date)) ~ FALSE,
(hour(date)>=16 & hour(date)<=19) ~ TRUE,
TRUE ~ FALSE)
)
date peak offpeak
1 1998-01-01 06:00:00 FALSE FALSE
2 1998-01-01 06:20:00 FALSE FALSE
3 1998-01-01 06:40:00 TRUE FALSE
4 1998-01-01 07:00:00 TRUE FALSE
5 1998-01-01 07:20:00 TRUE FALSE
6 1998-01-01 07:40:00 TRUE FALSE
7 1998-01-01 08:00:00 TRUE FALSE
8 1998-01-01 08:20:00 TRUE FALSE
9 1998-01-01 08:40:00 TRUE FALSE
10 1998-01-01 09:00:00 TRUE FALSE
11 1998-01-01 09:20:00 TRUE FALSE
12 1998-01-01 09:40:00 FALSE FALSE
13 1998-01-01 10:00:00 FALSE FALSE
30 1998-01-01 15:40:00 FALSE FALSE
31 1998-01-01 16:00:00 FALSE TRUE
32 1998-01-01 16:20:00 FALSE TRUE
33 1998-01-01 16:40:00 FALSE TRUE
34 1998-01-01 17:00:00 FALSE TRUE
35 1998-01-01 17:20:00 FALSE TRUE
36 1998-01-01 17:40:00 FALSE TRUE
37 1998-01-01 18:00:00 FALSE TRUE
38 1998-01-01 18:20:00 FALSE TRUE
39 1998-01-01 18:40:00 FALSE TRUE
40 1998-01-01 19:00:00 FALSE TRUE
41 1998-01-01 19:20:00 FALSE TRUE
42 1998-01-01 19:40:00 FALSE TRUE
43 1998-01-01 20:00:00 FALSE FALSE
数据:
data <- data.frame(date=seq.POSIXt(as.POSIXct("1998-01-01 06:00:00"),
as.POSIXct("1998-01-02 06:00:00"), by="20 min"))
这里我有一些在伦敦收集的每分钟空气污染浓度数据。这种污染物是一种与交通有关的空气污染物。我想添加另一个带有因子变量的列(Peak;Off-Peak)以区分在 期间收集的数据高峰时间(上午6:30-9:30)或非高峰时间(16:00至19:00)仅限周一至周五。当前时间列为 POSIXct 格式 date+time。
Sample Data
问题 1:我是否应该执行以下操作: 1.remove周六和周日数据 2.extract time 从 date+time 3.identify 时间范围 (6:30 am-9:30 am) 和 (16:00 到 19:00)
或者有一种方法可以使用 date+time (POSIXct)
来识别一天的时间范围QUESTION 2:How 我可以从 POSIXct 格式的 date+time 变量中正确提取 time ,可用于时间范围((6:30 am-9:30 am) and (16:00 to 19:00)) identification?
您可以使用 chron 包,因为它具有确定工作日的功能。它还具有假期功能。 lubridate 包可用于从 date/time 变量中提取小时和分钟。
library(chron)
library(lubridate)
data %>%
mutate(peak = case_when(
is.weekend(as.Date(date)) ~ FALSE,
(hour(date)==6 & minute(date)>=30 | hour(date)>6) &
(hour(date)==9 & minute(date)<=30 | hour(date)<9) ~ TRUE,
TRUE ~ FALSE),
offpeak = case_when(
is.weekend(as.Date(date)) ~ FALSE,
(hour(date)>=16 & hour(date)<=19) ~ TRUE,
TRUE ~ FALSE)
)
date peak offpeak
1 1998-01-01 06:00:00 FALSE FALSE
2 1998-01-01 06:20:00 FALSE FALSE
3 1998-01-01 06:40:00 TRUE FALSE
4 1998-01-01 07:00:00 TRUE FALSE
5 1998-01-01 07:20:00 TRUE FALSE
6 1998-01-01 07:40:00 TRUE FALSE
7 1998-01-01 08:00:00 TRUE FALSE
8 1998-01-01 08:20:00 TRUE FALSE
9 1998-01-01 08:40:00 TRUE FALSE
10 1998-01-01 09:00:00 TRUE FALSE
11 1998-01-01 09:20:00 TRUE FALSE
12 1998-01-01 09:40:00 FALSE FALSE
13 1998-01-01 10:00:00 FALSE FALSE
30 1998-01-01 15:40:00 FALSE FALSE
31 1998-01-01 16:00:00 FALSE TRUE
32 1998-01-01 16:20:00 FALSE TRUE
33 1998-01-01 16:40:00 FALSE TRUE
34 1998-01-01 17:00:00 FALSE TRUE
35 1998-01-01 17:20:00 FALSE TRUE
36 1998-01-01 17:40:00 FALSE TRUE
37 1998-01-01 18:00:00 FALSE TRUE
38 1998-01-01 18:20:00 FALSE TRUE
39 1998-01-01 18:40:00 FALSE TRUE
40 1998-01-01 19:00:00 FALSE TRUE
41 1998-01-01 19:20:00 FALSE TRUE
42 1998-01-01 19:40:00 FALSE TRUE
43 1998-01-01 20:00:00 FALSE FALSE
数据:
data <- data.frame(date=seq.POSIXt(as.POSIXct("1998-01-01 06:00:00"),
as.POSIXct("1998-01-02 06:00:00"), by="20 min"))