mardi 2 novembre 2021

How to make a conditional variable based on multiple variables?

Trying to make a catch all variable - so if a respondent answers "yes" to at least one of the 9 yes/no variables, then they will be placed into the "yes" category in the overall variable.

I've done this by:

overallvariable <- ifelse(df$v1 == "yes" | df$v2 == "yes" | df$v3 == "yes" | df$v4 == "yes" |df$v5 == "yes" | df$v6 == "yes" | df$v7 == "yes" | df$v8 == "yes" | df$v9 == "yes", "yes", "no")

However the table(overallvariable) comes up with:

no
##

instead of

yes no
### ##

Thank you for your help!

Note: everything seems to work until I add v9 Note: Just played around with where v9 goes in, it doesn't seem to be a problem attached to the variable as it produces the output I needed. So it seems to be an issue with adding a ninth condition.

Aucun commentaire:

Enregistrer un commentaire