id | age | city | cancer | tb | malaria |
---|---|---|---|---|---|
1 | 14 | A | Yes | No | No |
2 | 15 | B | No | No | Yes |
3 | 16 | C | No | No | No |
In the example above, cancer, tb, and malaria are diseases. How could I mutate a variable disease
such that it takes 1 when an observation has at least one disease otherwise 0. In my real data set, there are many more diseases. Instead of using cancer == "Yes"| tb == "Yes"| malaria == "Yes"
, is there a better alternative?
library(dplyr)
df <- tibble(
id = c(1:3),
age = c(14, 15, 16),
city = c("A", "B", "C"),
cancer = c("Yes", "No", "No"),
tb = c("No", "No", "No"),
malaria = c("No", "Yes", "No"),
)