Creating binary identifiers, based on condition of word combinations, for filtering in R

后端 未结 1 998
小鲜肉
小鲜肉 2021-01-25 01:10

Hi all I wish to know if this is possible. I am still exploring.

For example, I had this data set

Case     Date           Item
1        2016-03-25     Al         


        
相关标签:
1条回答
  • 2021-01-25 01:51

    We can try

     data %>% 
         group_by(Date) %>% 
         mutate(combiflag=any(grepl("Alpha", Item)) & any(grepl("Bravo", Item)))
    

    Or it can be

    data %>% 
         group_by(Date) %>% 
         mutate(combiflag= if(any(grepl("Alpha", Item)) & any(grepl("Bravo", Item))) 
                                grepl("Alpha|Bravo", Item) else FALSE)
    #   Case       Date    Item combiflag
    #  <int>      <chr>   <chr>     <lgl>
    #1     1 2016-03-25   Alpha      TRUE
    #2     2 2016-03-25   Bravo      TRUE
    #3     3 2016-03-25 Charlie     FALSE
    #4     4 2016-03-25   Delta     FALSE
    #5     5 2016-03-31   Alpha     FALSE
    #6     6 2016-03-31    Echo     FALSE
    #7     7 2016-03-31  Falcon     FALSE
    
    0 讨论(0)
提交回复
热议问题