samedi 16 novembre 2019

Is there an R function for nested ifelse functions?

I am trying to combine many variables to make a dummy variable for whether somebody belongs to one occupation category, using ifelse. I was wondering if there was a function to simplify this code and make it easier to repeat going forward. For example, my code is currently:

occupation_blue_collar <- ifelse(occupation=="Blue Collar", T, 
                          ifelse(occupation =="Blue Collar and Ex-Military", T, 
                          ifelse(occupation == "Blue Collar and Non-military Government", T,
                          ifelse(occupation== "Blue Collar and School Student", T,
                          ifelse(occupation== "Blue Collar and University Student", T,
                          ifelse(occupation== "Blue Collar and White Collar", T,       
                                        F))))))

I have to do this over many variables and many categories, so I was hoping there was a way to simplify. Thanks!

Aucun commentaire:

Enregistrer un commentaire