通过使用生产单位的时间戳,我想检查它产生的转移。基本上,生产每天进行两班制。轮班时间为06:00至18:00和18:00至06:00。下面的班次数据框显示了12月份轮班的计划。
让我说得更清楚
2015-12-01 A shift(2015-12-01 06:00:00 to 2015-12-01 17:59:59)
2015-12-01 D shift(2015-12-01 18:00:00 to 2015-12-02 05:59:59)
2015-12-02 A shift(2015-12-02 06:00:00 to 2015-12-02 17:59:59)
2015-12-02 D shift(2015-12-02 18:00:00 to 2015-12-03 05:59:59)
and so on..
head(shifts)
date day_shift night_shift
1 2015-12-01 A D
2 2015-12-02 A D
3 2015-12-03 B A
4 2015-12-04 B A
5 2015-12-05 C B
6 2015-12-06 C B
shifts <- structure(list(date = structure(1:31, .Label = c("2015-12-01",
"2015-12-02", "2015-12-03", "2015-12-04", "2015-12-05", "2015-12-06",
"2015-12-07", "2015-12-08", "2015-12-09", "2015-12-10", "2015-12-11",
"2015-12-12", "2015-12-13", "2015-12-14", "2015-12-15", "2015-12-16",
"2015-12-17", "2015-12-18", "2015-12-19", "2015-12-20", "2015-12-21",
"2015-12-22", "2015-12-23", "2015-12-24", "2015-12-25", "2015-12-26",
"2015-12-27", "2015-12-28", "2015-12-29", "2015-12-30", "2015-12-31"
), class = "factor"), day_shift = structure(c(1L, 1L, 2L, 2L,
3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L,
3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L), .Label = c("A",
"B", "C", "D"), class = "factor"), night_shift = structure(c(4L,
4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L,
4L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 1L, 1L, 2L, 2L, 3L), .Label = c("A",
"B", "C", "D"), class = "factor")), .Names = c("date", "day_shift",
"night_shift"), class = "data.frame", row.names = c(NA, -31L))
在检查数据框中,我有每个单位的时间戳。通过使用这些时间戳我想检查单位的生产转移。
head(check)
eventtime
1 2015-12-01 06:10:08
2 2015-12-01 10:10:24
3 2015-12-01 19:01:15
4 2015-12-02 01:54:54
5 2015-12-02 06:24:14
6 2015-12-02 08:15:47
check <- structure(list(eventtime = structure(c(1448946608, 1448961024,
1448992875, 1449017694, 1449033854, 1449040547, 1449076903, 1449085710,
1449100168, 1449119720), class = c("POSIXct", "POSIXt"), tzone = "")), .Names = "eventtime", row.names = c(NA,
-10L), class = "data.frame")
期望的结果:
ds
eventtime shift
1 2015-12-01 06:10:08 A
2 2015-12-01 10:10:24 A
3 2015-12-01 19:01:15 D
4 2015-12-02 01:54:54 D
5 2015-12-02 06:24:14 A
6 2015-12-02 08:15:47 A
7 2015-12-02 18:21:43 D
8 2015-12-02 20:48:30 D
9 2015-12-03 00:49:28 D
10 2015-12-03 06:15:20 B
为了保持简单,我只显示了12月份的轮班计划。实际上我需要检查整年。
答案 0 :(得分:1)
这是使用lubridate及其%within%
函数检查日期是否在某个区间内的答案。根据您的原始数据是否实际存储为因子,您可以通过删除某些转换来简化代码。
library(lubridate)
day_shift_start <- as.POSIXct(shifts$date) + hms("06:00:00")
day_shift_end <- as.POSIXct(shifts$date) + hms("17:59:59")
night_shift_start <- as.POSIXct(shifts$date) + hms("18:00:00")
night_shift_end <- as.POSIXct(shifts$date) + days(1) + hms("05:59:59")
shift_intervals <- data.frame(intervals = c(interval(day_shift_start, day_shift_end),
interval(night_shift_start, night_shift_end)),
shift = c(as.character(shifts$day_shift),
as.character(shifts$night_shift)))
check$shift <- unlist(lapply(check$eventtime, function(x) {
shift_intervals$shift[x %within% shift_intervals$intervals]
}))
check
# eventtime shift
# 1 2015-12-01 06:10:08 A
# 2 2015-12-01 10:10:24 A
# 3 2015-12-01 19:01:15 D
# 4 2015-12-02 01:54:54 D
# 5 2015-12-02 06:24:14 A
# 6 2015-12-02 08:15:47 A
# 7 2015-12-02 18:21:43 D
# 8 2015-12-02 20:48:30 D
# 9 2015-12-03 00:49:28 D
# 10 2015-12-03 06:15:20 B