Extract values based on conidtion of another column

前端 未结 2 551
梦谈多话
梦谈多话 2021-01-29 11:25
case_number <- c(\"1\", \"1\", \"2\", \"2\", \"2\", \"3\", \"3\") 
type <- c(\"STD\", \"STD2\", \"STD\", \"STD3\", \"STD2\", \"STD\", \"STD2\") 
date <- as.Date         


        
2条回答
  •  半阙折子戏
    2021-01-29 11:35

    Assuming every case_number would have both the values , another option is to check the position of "STD" and "STD2" and select groups where the difference is equal to 1.

    check_fun <- function(x) {
       abs(diff(c(which.max(x == "STD"), which.max(x == "STD2")))) == 1
    }
    
    library(dplyr)
    
    data %>% group_by(case_number) %>% filter(check_fun(type))
    
    # case_number type  date      
    #              
    #1 1           STD   2008-11-01
    #2 1           STD2  2009-03-25
    #3 3           STD   2015-03-14
    #4 3           STD2  2015-04-15
    

    Or if you just need the unique case_number

    data %>% 
      group_by(case_number) %>% 
      filter(check_fun(type)) %>% 
      pull(case_number) %>% 
      unique
    
    #[1] 1 3
    #Levels: 1 2 3
    

提交回复
热议问题