我试图根据上一行的值来估算时间。
Concat ID Date Time1 Time2
1615 - 2019-05-14 1615 5/14/2019 20:57:56 21:26:45
161 - 2019-05-14 161 5/14/2019 21:52:19 NA
161 - 2019-05-15 161 5/15/2019 NA 1:10:49
161 - 2019-05-14 161 5/17/2019 21:52:19 NA
161 - 2019-05-15 161 5/20/2019 NA 1:10:49
对于每个ID列,如果日期差小于2,并且如果Time1为NA,而先前的相邻Time2为NA,那么我想替换Time1中的“ 00:00:01”和“ 23:59:59” ”,如下所示。
Concat ID Date Time1 Time2
1615 - 2019-05-14 1615 5/14/2019 20:57:56 21:26:45
161 - 2019-05-14 161 5/14/2019 21:52:19 23:59:59
161 - 2019-05-15 161 5/15/2019 00:00:01 1:10:49
161 - 2019-05-14 161 5/17/2019 21:52:19 NA
161 - 2019-05-15 161 5/20/2019 NA 1:10:49
我尝试使用dplyr的超前和滞后值,但无法正确获取
答案 0 :(得分:0)
缺少可重复的数据示例,我只能猜测这将对您有所帮助:
library(dplyr)
library(lubridate)
df %>%
group_by(ID) %>%
mutate(
Time1_fixed = ifelse(
(lag(Date,1)+1 == Date) & # the previous Date was one day before this one
is.na(Time1) & # AND this Time1 is NA
is.na(lag(Time2,1)), # AND the previous Time1 was NA
'00:00:01',
Time1
),
Time2_fixed = ifelse(
(lead(Date,1) == Date+1) & # the next Date is one day after this one
is.na(Time2) & # AND this Time2 is NA
is.na(lead(Time1,1)), # AND the next Time1 is NA
'23:59:59',
Time2
)
)