Search code examples
rtidyverse

Filtering values from a vector found in a string with combinations of those values


I have the following tibble and vector:

library(tidyverse)

mytibble <- tibble(A = c("Big Change", "Small change, New value, Strange Value", "NA-Value"))

mychoice <- c("Small Change", "NA-Value")

I would like to filter all the values in my tibble that contain the strings in the vector mychoice. The following is clearly not working as the second row A is not filtered.

mytibble %>% 
  filter(A %in% mychoice)

I tried with str_detect but didn't manage to find an answer.


Solution

  • Assuming the typo (Change vs change) is accidental:

    library(tidyverse)
    
    mytibble <- tibble(A = c("Big Change", "Small change, New value, Strange Value", "NA-Value"))
    
    mychoice <- c("Small change", "NA-Value") #edited capital C
    
    mytibble |> filter(str_detect(A, paste(mychoice, collapse = "|")))
    

    yields:

    # A tibble: 2 × 1
      A                                     
      <chr>                                 
    1 Small change, New value, Strange Value
    2 NA-Value    
    

    This is because str_detect expects a single string as its pattern rather than a vector, so we collapse the vector down using the | symbol to represent "or".