case_number <- c("1", "1", "2", "2", "2", "3", "3", "4", "4")
action <- c("A1", "A2", "A3", "A1", "A2", "A2", "A2", "A3", "A2")
date <- as.Date(c('2008-11-1','2009-3-25','2010-3-14',"2010-10-14","2010-11-14", '2015-3-14', '2015-4-15', '2016-09-01', '2016-01-01'))
data <- data.frame(case_number,action, date)
我在解决如何按日期将彼此相邻的case_numbers
和action
中的任何A1
提取唯一A2
时遇到问题。因此,对于此示例,它只有1和2。
答案 0 :(得分:1)
一种选择是检查'action'中的lead
值是否为'A2',并且当前行值为'A1'至filter
这些行,然后pull
case_number
,并在需要时使用distinct
(在pull
之前)或之后使用unique
library(dplyr)
data %>%
filter(action == "A1" & lead(action == "A2")) %>%
pull(case_number)
#[1] 1 2
#Levels: 1 2 3 4