r——基于条件的脉冲时间



我正试图根据前一行的值估算时间。

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"和上一个邻近的NA中的"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的超前和滞后值,但无法正确获得

缺少一个可复制的数据示例,我只能猜测这会对您有所帮助:

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
)
)

最新更新