Creating binary column based is value is present in any column

I am working with data where co-morbidities are coded for individuals in multiple columns and I want to create code that produces a binary column if it is present (1=disease present, 0=not present). For each individual they have multiple columns in which this could be in as well as the diagnoses being listed randomly and in no order. Also for each illness there may be multiple codes which equate to the same diagnosis.

For example, say there are two patients and each has 10 diagnosed diseases which are shown as columns, for example disease_1, disease_2, etc.. Disease X may be coded with I10 or B42. For the first individual I10 may be in the second column and for the second individual B42 may be in the seventh column. The result of the code would be that both individuals have a 1 in a newly created column.

I have tried using the mutate function of tidyverse and starts_with but am struggling to produce code. I have a rough mock-up of what I have tried but don't think I am particularly close. I would be grateful for any help.

Date_disease_column <- df %>%
mutate(Disease= case_when(
startsWith(if_any(.cols = starts_with("disease"), "I10") ~ "1"))

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)

That's great, thanks! Really helpful.

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.