我想根据时间戳更改数据项的日期。基本上,公司从早上7点开始运营,一直工作到第二天早上6点59分。整个持续时间计为1天。因此,基本上,2020年11月19日将意味着从2020年11月份19日07:00至2020年12月20日06:59
我有这个数据集
structure(list(Timestamp = structure(c(1593714765.03054, 1593714824.99918,
1593714884.96776, 1593714945.01468, 1593715005.01454, 1593715064.98314,
1593715125.03002, 1593715185.18608, 1593715244.99822, 1593715304.96657
), class = c("POSIXct", "POSIXt"), tzone = "Asia/Kolkata"), Date = structure(c(18446,
18446, 18446, 18446, 18446, 18446, 18446, 18446, 18446, 18446
), class = "Date"), Time = c("00:02", "00:03", "00:04", "00:05",
"00:06", "00:07", "00:08", "00:09", "00:10", "00:11"), `HCM 18` = c(7,
8, 7, 8, 7, 7, 8, 7, 8, 7), `HCM 19` = c(8, 8, 8, 8, 8, 7, 8,
8, 8, 8), `HCM 20` = c(7, 7, 7, 7, 8, 7, 4, 8, 7, 7), `HCM 21` = c(8,
7, 8, 8, 8, 7, 8, 8, 8, 8), `HCM 22` = c(8, 7, 8, 7, 7, 8, 7,
8, 7, 7), `HCM 23` = c(8, 7, 8, 8, 8, 7, 8, 8, 8, 8), `HCM 24` = c(7,
7, 7, 6, 7, 7, 7, 6, 4, 0), `HCM 25` = c(8, 8, 8, 7, 8, 8, 8,
7, 8, 8), `HCM 54` = c(7, 8, 8, 7, 6, 8, 7, 8, 8, 7), `HCM 55` = c(8,
7, 8, 7, 8, 8, 7, 8, 8, 7), `HCM 56` = c(8, 8, 8, 7, 8, 8, 7,
8, 8, 7), `HCM 57` = c(8, 8, 7, 8, 8, 7, 8, 8, 7, 8), `HCM 58` = c(8,
8, 7, 8, 7, 8, 7, 8, 7, 8), `HCM 59` = c(8, 1, 6, 8, 8, 7, 8,
8, 7, 8), `HCM 60` = c(8, 7, 8, 8, 7, 8, 8, 7, 8, 8)), row.names = c(NA,
10L), class = "data.frame")
所以现在我需要将日期更改为前一天早上7点以内的时间。
我想在R.做这个
您可以使用data.table
包中的ITime
类。ITime
可以将一天中的时间存储为一天中秒的整数。创建一个变量open_time
来存储上午7:00的秒数。然后,对于每个Timestamp
(转换为ITime
(,查看时间是否小于open_time
,如果小于,则使用Date - 1
而不是Date
。为了进行演示,将创建一个新列(NewDate
(。
library(dplyr)
library(lubridate)
library(data.table)
open_time <- as.ITime(as.POSIXct("07:00", format = "%H:%M"))
df %>%
mutate(NewTime = as.ITime(Timestamp),
NewDate = if_else(
NewTime < open_time,
Date - 1,
Date
)
)