Search code examples
rfiltergrepl

Filter all words with the same character length except some specific words


I know I can remove all word items that have just 2 characters with the following order:

data %>% filter(str_length(word) != 2)

In my Case I would like to filter all items with 2 characters but some specific words like "EU" should be still in the tibble. Is it possible to define some expections to the order above?


Solution

  • Say you want to filter all two-letters word except "EU" and "UE":

    test <- tibble(word=c("Word", "Another word", "Wo", "Wa", "EU","UE"))
    
    test
    
    # A tibble: 6 x 1
      word        
      <chr>       
    1 Word        
    2 Another word
    3 Wo          
    4 Wa          
    5 EU          
    6 UE   
    
    test %>% filter(ifelse(str_length(word)==2 & !grepl("EU|UE", word), FALSE, TRUE))
    
     A tibble: 4 x 1
      word        
      <chr>       
    1 Word        
    2 Another word
    3 EU          
    4 UE