【发布时间】:2018-09-15 21:13:14
【问题描述】:
试图以这样的方式操纵timestamp变量:如果下一个activity的开始时间在前一个activity的结束时间之前,那么将前一个activity的开始和结束时间更新为下一个activity的前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")))
【问题讨论】: