DT是具有POSIXct列“date_and_time”的data.table。我如何使用data.table语法添加一个3级因子列,指示每个“date_and_time”实例属于哪个班次?
DT <- data.table(date_and_time = as.POSIXct(
c("1997-07-01 23:00",
"1999-01-01 02:05",
"2006-01-01 12:00",
"2009-01-01 22:30",
"2012-07-01 03:59"), required.components = 5L)
)
shifts_delimiter = c("00:00", "08:00", "16:00")
所需结果:
> DT
date_and_time shift
1: 1997-07-01 23:00:00 3
2: 1999-01-01 02:05:00 1
3: 2006-01-01 12:00:00 2
4: 2009-01-01 22:30:00 3
5: 2012-07-01 03:59:00 1