I have a character variable that contains entries representing diagnoses. I would like to create a variable to indicate whether a specific diagnosis is present.
For example, in the below data frame I have a variable called Diagnoses. This variable contains strings of numbers, whereby the numbers represent different diagnoses (e.g., 1 = diabetes, 2 = depression). I want to code a new variable to indicate whether the character variable contains a number I am interested in (e.g., 2 = depression).
Diagnoses Relevant_Diagnoses
1,3,5,7,8 0
1,2,3,4 1
2,3 1
3,5,8 0
I've got the below code, but am aware I need to change the last ifesle statement so that it checks whether 2 is included in the list (rather than ==2), but I'm not sure how. What is the best way to go about this?
df$Relevant_Diagnoses <- ifelse(is.na(df$Diagnoses), NA, ifelse(df$Diagnoses == 2, 1, 0))
Many thanks for your help!
Aucun commentaire:
Enregistrer un commentaire