How to extract all the rows if a level in one column contains all the levels of another column in R?

前端 未结 3 860
你的背包
你的背包 2020-12-12 07:38

I have the following data

    ID       INDUSTRY        PRODUCT                  
  625109    PersonalCare     Neolone Preservatives    
  199672    PersonalC         


        
3条回答
  •  执念已碎
    2020-12-12 07:58

    This should work:

    library(dplyr)
    
    df <- data.frame(ID = c(62, 19, 22, 18, 62, 22),
                     INDUSTRY = c("PC", "PC", "P", "F", "PC", "F"),
                     PRODUCT = c("NP", "NP", "O", "SB", "O", "NP"))
    
    df %>% 
      group_by(ID) %>% 
      filter(any(PRODUCT %in% c("NP"))& any(PRODUCT %in% c("O"))) 
    
    # A tibble: 4 x 3
    # Groups:   ID [2]
         ID INDUSTRY PRODUCT
           
    1    62       PC      NP
    2    22        P       O
    3    62       PC       O
    4    22        F      NP
    

提交回复
热议问题