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

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

I have the following data

    ID       INDUSTRY        PRODUCT                  
  625109    PersonalCare     Neolone Preservatives    
  199672    PersonalC         


        
相关标签:
3条回答
  • 2020-12-12 07:55

    you can do it with the library dplyr

    filteredData<-data %>%
    filter(INDUSTRY=='PersonalCare',PRODUCT=='Optiphen')
    
    0 讨论(0)
  • 2020-12-12 07:57

    Multiple ways to do this :

    In dplyr

    df %>% 
       group_by(ID) %>% 
       filter(all(c("Neolone Preservatives", "Optiphen") %in% PRODUCT))
    
    
    #     ID     INDUSTRY               PRODUCT
    #   <int>        <chr>                 <chr>
    #1 625109 PersonalCare Neolone Preservatives
    #2 227047       Pharma              Optiphen
    #3 625109 PersonalCare              Optiphen
    #4 227047         Food Neolone Preservatives
    

    In base R :

    df[ave(df$PRODUCT, df$ID, FUN = function(x) 
                     all(c("Neolone_Preservatives", "Optiphen") %in% x)) == "TRUE", ]
    
    0 讨论(0)
  • 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
      <dbl>   <fctr>  <fctr>
    1    62       PC      NP
    2    22        P       O
    3    62       PC       O
    4    22        F      NP
    
    0 讨论(0)
提交回复
热议问题