根据另一列的条件获取值

时间:2019-12-04 15:40:42

标签: r

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_numbersaction中的任何A1提取唯一A2时遇到问题。因此,对于此示例,它只有1和2。

1 个答案:

答案 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