vendredi 29 janvier 2016

multiple criteria from more than one column to create a new categorical variable

Normally if I want to assign a categorical variable based on the value of another variable I do something like this:

iris$Criteria <- ifelse(iris$Petal.Width>0.3,"High","Low")

However, I am not sure how to accomplish this task if I am basing the categorical variable on the value of two variables. I can do it like this:

iris$Criteria <- ifelse(iris$Petal.Width>0.3 & iris$Species=="setosa","High",
                    ifelse(iris$Petal.Width>1.3 & iris$Species=="versicolor","V_High","Low"))

But for a large dataset with many such comparisons the code get really clunky and more important almost unreadable. I am trying to make code that people can see the criteria I applied very quickly and easily. So I am wondering if people have a better strategy to do this.

Any help would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire