gpt4 book ai didi

r,根据以下记录的值更新时间戳

转载 作者:行者123 更新时间:2023-12-02 01:02:29 24 4
gpt4 key购买 nike

尝试以这种方式操作时间戳变量:如果后续事件的开始时间早于上一个事件的结束时间,则将上一个事件的开始和结束时间更新为下一个事件之前的 1 秒.

补充说明:

一个事件可以在同一个工作中重复;即事件“A”。

有些单独的事件有相同的开始和结束时间,有些不同。这是我故意做的;你可以忽略这个。

workID  workActivityID activity     status             timestamp      timestampDesired
1 1 A start 2018-01-01 09:55:01 2018-01-01 09:54:05
1 1 A end 2018-01-01 09:55:01 2018-01-01 09:54:05
1 2 B start 2018-01-01 09:54:06 2018-01-01 09:54:06
1 2 B end 2018-01-01 09:56:22 2018-01-01 09:56:22
1 3 C start 2018-01-01 09:57:22 2018-01-01 09:57:22
1 3 C end 2018-01-01 09:57:22 2018-01-01 09:57:22
1 4 A start 2018-02-02 08:35:00 2018-02-02 08:35:00
1 4 A end 2018-02-02 08:35:00 2018-02-02 08:35:00
2 1 A start 2018-02-02 08:13:55 2018-02-02 08:14:01
2 1 A end 2018-02-02 08:14:20 2018-02-02 08:14:01
2 2 B start 2018-02-02 08:14:02 2018-02-02 08:14:02
2 2 B end 2018-02-02 08:14:50 2018-02-02 08:14:50
2 3 C start 2018-02-02 10:00:00 2018-02-02 10:00:00
2 3 C end 2018-02-02 10:00:00 2018-02-02 10:00:00
2 4 A start 2018-02-02 10:22:00 2018-02-02 10:22:00
2 4 A end 2018-02-02 10:24:00 2018-02-02 10:24:00

数据:

library(lubridate)
df <-
data.frame(
workID = rep(c(1,2), each=8),
workActivityID = rep(c(1,2,3,4), each=2, times=2),
activity = rep(c("A","B","C","A"), each=2, times=2),
startEnd = rep(c("start", "end"), times=8),
timestamp = ymd_hms(c("2018-01-01 09:55:01", "2018-01-01 09:55:01", "2018-01-01 09:54:06", "2018-01-01 09:56:22", "2018-01-01 09:57:22", "2018-01-01 09:57:22", "2018-02-02 08:35:00","2018-02-02 08:35:00",
"2018-02-02 08:13:55", "2018-02-02 08:14:20", "2018-02-02 08:14:02", "2018-02-02 08:14:50", "2018-02-02 10:00:00", "2018-02-02 10:00:00", "2018-02-02 10:22:00", "2018-02-02 10:24:00")),
timestampDesired = ymd_hms(c("2018-01-01 09:54:05", "2018-01-01 09:54:05", "2018-01-01 09:54:06", "2018-01-01 09:56:22", "2018-01-01 09:57:22", "2018-01-01 09:57:22", "2018-02-02 08:35:00", "2018-02-02 08:35:00",
"2018-02-02 08:14:01", "2018-02-02 08:14:01", "2018-02-02 08:14:02", "2018-02-02 08:14:50", "2018-02-02 10:00:00", "2018-02-02 10:00:00", "2018-02-02 10:22:00", "2018-02-02 10:24:00")))

最佳答案

可以使用 tidyr::spreadtidyr::gather 找到可能的解决方案。从某种意义上说,该方法很简单,将 startend 移动到同一行,这样决策和更改操作(如果需要)将更容易。执行修改后,将其改回长格式。

library(tidyverse)

df %>% select(-timestampDesired) %>%
spread(startEnd, timestamp) %>%
group_by(workID) %>%
mutate(start = as.POSIXct(ifelse(!is.na(lead(start)) & lead(start) < end,
lead(start) - 1, start), origin = "1970-01-01 00:00:00" )) %>%
mutate(end = as.POSIXct(ifelse(!is.na(lead(start)) & lead(start) < end,
lead(start) - 1, end), origin = "1970-01-01 00:00:00" )) %>%
ungroup() %>%
gather("startEnd", "timestamp", c("start","end")) %>%
arrange(workID, workActivityID, desc(startEnd)) %>%
as.data.frame()

# workID workActivityID activity startEnd timestamp
# 1 1 1 A start 2018-01-01 09:54:05
# 2 1 1 A end 2018-01-01 09:54:05
# 3 1 2 B start 2018-01-01 09:54:06
# 4 1 2 B end 2018-01-01 09:56:22
# 5 1 3 C start 2018-01-01 09:57:22
# 6 1 3 C end 2018-01-01 09:57:22
# 7 1 4 A start 2018-02-02 08:35:00
# 8 1 4 A end 2018-02-02 08:35:00
# 9 2 1 A start 2018-02-02 08:14:01
# 10 2 1 A end 2018-02-02 08:14:01
# 11 2 2 B start 2018-02-02 08:14:02
# 12 2 2 B end 2018-02-02 08:14:50
# 13 2 3 C start 2018-02-02 10:00:00
# 14 2 3 C end 2018-02-02 10:00:00
# 15 2 4 A start 2018-02-02 10:22:00
# 16 2 4 A end 2018-02-02 10:24:00

关于r,根据以下记录的值更新时间戳,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49682664/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com