I have a dataframe like this:
BQPREVIO ASMAPREVIO bronquitis_cronica expectoración silibancias ENFISEMA PREVIO
1 2 1 1 1 1
2 2 1 1 1 1
2 2 1 1 2 1
1 2 1 2 1 1
1 2 1 1 2 1
1 2 1 1 2 1
2 1 1 1 2 1
2 2 1 1 2 1
1 1 1 1 2 1
1 2 1 2 1 1
These values are worth 2 = Yes and 1 = No
I want to categorize by levels:
Tipo I Todos No
Tipo II Efisema Si
Tipo III Broquitis crónica o expectoración o BQPREVIO
Tipo IV Silibancias o ASMAPREVIO
I have tried to solve this rpboema by joining each row with the unite function:
BD_table6_1<-unite(BD_table6_1,total,c(1,2,3,4,5,6),sep="_",remove= FALSE)
BD_table6_1$GESEPOCFENOTIPO_TipoI<-ifelse(BD_table6_1$total == "1_1_1_1_1_1" , "1",
ifelse(BD_table6_1$total == "1_1_1_1_1_2" , "2",
ifelse(BD_table6_1$total == "1_1_2_1_1_1" , "3",
ifelse(BD_table6_1$total == "1_1_1_2_1_1" , "3",
ifelse(BD_table6_1$total == "2_1_1_1_1_1" , "3",
ifelse(BD_table6_1$total == "1_1_2_2_1_1" , "3",
ifelse(BD_table6_1$total == "2_1_2_1_1_1" , "3",
ifelse(BD_table6_1$total == "2_1_2_1_1_1" , "3",
ifelse(BD_table6_1$total == "1_2_1_1_2_1" , "4", "0")))))))))
Although it works, I am left with a horrible line of code. Is there a way to do it that isn't so cumbersome?
Thank you very much in advance.
This is a good case to use
case_when()
, because it allows you to write each condition and evaluate it sequentially:Now, if the conditions grew in quantity, it might be more practical to have everything parameterized in a
data.frame
child with the characterization of each value ofGESEPOCFENOTIPO_TipoI