Lets say I have multiple variables that measure substance abuse i.e a1 is on alcohal usage, a2 is on bhang and a3 is on cocaine. I would like to generate variable afin that indicates engaged in substance abuse if any of the the three is yes.
Is there a way to shorten the code so I don't specify use multiple ifelse statements as below? Trying to find the best way to do it because I have more than 10 variables to collapse into one and writing ifelse may not be ideal.
# Anymatch
library(tidyverse)
set.seed(2021)
mydata <- tibble(
a1 = factor(round(runif(20, 1, 3)),
labels = c("Yes", "No", "N/A")),
a2 = factor(round(runif(20, 1, 3)),
labels = c("Yes", "No", "N/A")),
a3 = factor(round(runif(20, 1, 3)),
labels = c("Yes", "No", "N/A")),
b1 = round(rnorm(20, 10, 2)))
mydata
mydata <- mydata %>%
mutate(afin = ifelse(a1 == "Yes"|a2=="Yes"|a3=="Yes", "Yes", "No"))