How to apply filter of multiple conditions to multiple variables and see resulting list of values?

Would this work for you?

library(purrr)
library(tibble)
target <- c("f11", "t40") %>% # either/or
  paste(collapse = "|")

target
#> [1] "f11|t40"

# data
my_data <- tribble(
  ~p_dx, ~dx_1, ~dx_2, ~dx_3,
  "f11", "t401", NA, NA,
  "f11", "t402", "f12", "t41",
  "f01", "t01", "f111", "t401",
  "f02", "t402", NA, NA,
  "t40", "f111", NA, NA
)
LIST <- map(my_data,.f=function(COL){
  tmp <- unique(COL)
  tmp[grepl(target,tmp)]
})
unique(unlist(LIST))
#> [1] "f11"  "t40"  "t401" "t402" "f111"

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

1 Like