Hi,
Welcome to the RStudio community!
You can do this using the apply() function and looping over all the rows checking if the value you are interested in is present. Here is an example:
#Generate some data
myData = data.frame(
DX1 = LETTERS[1:5],
DX2 = LETTERS[4:8],
DX3 = LETTERS[c(3,5,1,9,11)]
)
myData
#> DX1 DX2 DX3
#> 1 A D C
#> 2 B E E
#> 3 C F A
#> 4 D G I
#> 5 E H K
# Example: Hypertension = A
myData$hypertension = apply(myData, 1, function(row){
ifelse("A" %in% row, 1, 0)
})
myData
#> DX1 DX2 DX3 hypertension
#> 1 A D C 1
#> 2 B E E 0
#> 3 C F A 1
#> 4 D G I 0
#> 5 E H K 0
The apply function will loop over all rows (1, we used this) or columns (2) of a data frame performing a function you provide (here I created one).
Created on 2022-06-01 by the reprex package (v2.0.1)
Hope this helps,
PJ