I have a data set with multiple diagnosis columns (ie. DIAG1, DIAG2, DIAG3, etc.). I am looking to create a loop that will check each column for all of my rows, but I'm looking for more than one diagnosis code within each of those columns.
For example, I want to find code xxx1 and xxx3 if present in DIAG1, DIAG2, DIAG3, etc.
My code is below where
1. df = my dataframe
2. df$illness = is the variable I want to create
3. xxxx1 = the code I'm looking for
4. [26:34, 57:72] = the columns where DIAG1, etc. exist
**EDIT: Example data:
DIAG3 DIAG4 DIAG5 DIAG6
1231 xxx1 5468 5468
1454 2352 4542 4864
xxx2 1235 1234 3564
1234 1589 xxx1 8498
Code I tried to perform:
for (i in 1:nrow(df)) {
df$illness[i] <- ("xxx1" %in% df[i,26:34, 57:72] | "xxx3" %in%
df[i,26:34, 57:72]}
What I would like my loop to perform:
DIAG3 DIAG4 DIAG5 DIAG6 Illness
1231 xxx1 5468 5468 TRUE
1454 2352 4542 4864 FALSE
xxx3 1235 1234 3564 TRUE
1234 1589 xxx1 8498 TRUE
What happens is that the code runs but never ends. I don't know where my mistake is. Thank you