使用dplyr的超前和滞后向量中的微分值

时间:2018-08-30 09:08:00

标签: r dplyr lag lead

我有一个数据框

structure(list(Time = structure(c(1531056854, 1531057121, 1517382101, 
1517386850, 1517386951, 1517399987, 1517400523, 1517400523), class = c("POSIXct", 
"POSIXt")), Data = c("Start", "Exit", "Start", "Start", "Exit", 
"Start", "Exit", "Exit"), same = c(0, 0, 1, 0, 0, 0, 1, NA)), class = "data.frame", .Names = c("Time", 
"Data", "same"), row.names = c(NA, -8L))

第2列的理想情况是在Start之后加上Exit

但是,在某些情况下,我可以有一个Start``StartExit或一个Start后跟Exit``Exit。我试图通过此代码确定随后的开始和退出:

library(dplyr)
df <- df %>% mutate(same = ifelse(Data == lead(Data), 1, 0))

这为我提供了以下输出:

                  Time  Data same
1 2018-07-08 19:04:14 Start    0
2 2018-07-08 19:08:41  Exit    0
3 2018-01-31 12:31:41 Start    1
4 2018-01-31 13:50:50 Start    0
5 2018-01-31 13:52:31  Exit    0
6 2018-01-31 17:29:47 Start    0
7 2018-01-31 17:38:43  Exit    1
8 2018-01-31 17:38:43  Exit   NA

我试图弄清楚如果顺序中有两个Start第一个{{1},该如何识别第二个Start } ,如果序列中有两个Exit,标记为1。所需的输出如下:

Exit

我尝试在 Time Data same 1 2018-07-08 19:04:14 Start 0 2 2018-07-08 19:08:41 Exit 0 3 2018-01-31 12:31:41 Start 0 4 2018-01-31 13:50:50 Start 1 #this should be one 5 2018-01-31 13:52:31 Exit 0 6 2018-01-31 17:29:47 Start 0 7 2018-01-31 17:38:43 Exit 1 #this should be one 8 2018-01-31 17:38:43 Exit 0 中使用if条件,但是情况变得混乱了。

2 个答案:

答案 0 :(得分:4)

library(tidyverse)
df %>% 
  mutate( same2 = ifelse( Data == "Start" & lag( Data ) == Data, 1, 0 )) %>%
  mutate( same2 = ifelse( Data == "Exit" & lead( Data ) == Data, 1, same2 ) )

#                  Time  Data same same2
# 1 2018-07-08 15:34:14 Start    0    NA
# 2 2018-07-08 15:38:41  Exit    0     0
# 3 2018-01-31 08:01:41 Start    1     0
# 4 2018-01-31 09:20:50 Start    0     1
# 5 2018-01-31 09:22:31  Exit    0     0
# 6 2018-01-31 12:59:47 Start    0     0
# 7 2018-01-31 13:08:43  Exit    1     1
# 8 2018-01-31 13:08:43  Exit   NA    NA

答案 1 :(得分:1)

我们可以用as.integer来强制逻辑转换为二进制。

df %>% 
    mutate(same2 = as.integer((Data == 'Start' & lag(Data) == Data)|
                              (Data == 'Exit' &  lead(Data) == Data)))