This is just an issue of syntax I believe!
library(dplyr)
#>
#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>
#> filter, lag
#> The following objects are masked from 'package:base':
#>
#> intersect, setdiff, setequal, union
dat = tibble(
patient = 1:5,
d1 = c("a", "b", "B42", "d", "e"),
d2 = c("I10", "b", "c", "d", "e"),
d3 = c("a", "B42", "c", "d", "e")
)
dat |>
mutate(
flag = case_when(
if_any(.cols = starts_with("d"),
.fns = ~.x %in% c("I10", "B42")) ~ 1,
TRUE ~ 0
))
#> # A tibble: 5 x 5
#> patient d1 d2 d3 flag
#> <int> <chr> <chr> <chr> <dbl>
#> 1 1 a I10 a 1
#> 2 2 b b B42 1
#> 3 3 B42 c c 1
#> 4 4 d d d 0
#> 5 5 e e e 0
Created on 2021-12-28 by the reprex package (v2.0.1)