Using this data.frame
Data
df <- read.table(text = c("
ID cat1 cat2 cat3
site1 High High High
site1 High High Medium
site1 High High Low
site1 High Medium High
site1 High Medium Medium
site1 High Medium Low
site1 High Low High
site1 High Low Medium
site1 High Low Low
site1 Medium High High
site1 Medium High Medium
site1 Medium High Low
site1 Medium Medium High
site1 Medium Medium Medium
site1 Medium Medium Low
site1 Medium Low High
site1 Medium Low Medium
site1 Medium Low Low
site1 Low High High
site1 Low High Medium
site1 Low High Low
site1 Low Medium High
site1 Low Medium Medium
site1 Low Medium Low
site1 Low Low High
site1 Low Low Medium
site1 Low Low Low
"), header =T)
I want to create a new column called "new_category" based on cat1
, cat2
and cat3
.
I want each row in new_category
to be have the common class "or word" in cat1
, cat2
and cat3
. If all values are different (High, Medium and Low), new_category
will take the highest class (High
in this case).
For example
If
cat1 = High
,cat2 = High
,cat3= Medium
, thennew_category = High
If
cat1 = High
,cat2 = Medium
,cat3= Low
, thennew_category = High
If
cat1 = Medium
,cat2 = Medium
,cat3= Low
, thennew_category = Medium
I can do this using ifelse
. However, there are many combinations of cat1
and cat2
and cat3
.
Any suggestion for a faster or easier way to do that?
Aucun commentaire:
Enregistrer un commentaire