use a character vector to filter a data

Use the | character, meaning "or".

library(tidyverse)

# target criteria
target <- c("a", "b", "c") %>% paste(collapse = "|")

target
#> [1] "a|b|c"

# my data 
my_data <- tribble(
  ~col1, ~col2, ~col3,
  "aaa", "aaa", "aaa",
  "bbb", "bbb", "bbb",
  "ccc", "ccc", "ccc",
  "ddd", "ddd", "ddd",
  "eee", "eee", "eee",
  "fff", "fff", "fff"
)

# filter
my_filtered_data <- my_data %>%
  filter(str_detect(col1, target))

my_filtered_data
#> # A tibble: 3 x 3
#>   col1  col2  col3 
#>   <chr> <chr> <chr>
#> 1 aaa   aaa   aaa  
#> 2 bbb   bbb   bbb  
#> 3 ccc   ccc   ccc

Created on 2021-12-27 by the reprex package (v2.0.1)

2 Likes