# Multiple condition to create categorical variable

Let say I have a database (dat) including ID variable and two quantitative continuous variables on several times (peso1_v00 - baseline, peso1_v66 - 6 months / cintura1_v00 - baseline, cintura1_v66 - 6 months)

I want to create another variable based on the values of weight and waist circumference. If the measurement at 6 months has reduced in x % the category is y if .....

``````dat %>%
dplyr::mutate(i_ratiolg_cat = if(peso1_v66 <=   peso1_v00*0.92 & cintura1_v66 <= cintura1_v00 * 0.95) {
"Total success"
} else if(peso1_v66 <=   peso1_v00*0.92 & cintura1_v66 > cintura1_v00 * 0.95){
"Weight success"
} else if(peso1_v66 >   peso1_v00*0.92 & cintura1_v66 <= cintura1_v00 * 0.95){
"Waist success"
} else if (peso1_v66 >   peso1_v00*0.92 & cintura1_v66 > cintura1_v00 * 0.95)
{
"Fail" })
``````

la condici�n tiene longitud > 1 y s�lo el primer elemento ser� usadola condici�n tiene longitud > 1 y s�lo el primer elemento ser� usadola condici�n tiene longitud > 1 y s�lo el primer elemento ser� usadola condici�n tiene longitud > 1 y s�lo el primer elemento ser� usado

I am not sure what I am doing wrong but it is not working

Hi @Javier9 ,
Try this:

``````library(dplyr)
dat %>%
mutate(i_ratiolg_cat = case_when(
peso1_v66 <= peso1_v00 * 0.92 & cintura1_v66 <= cintura1_v00 * 0.95 ~ "Total success",
peso1_v66 <= peso1_v00 * 0.92 & cintura1_v66 > cintura1_v00 * 0.95 ~ "Weight success",
peso1_v66 > peso1_v00 * 0.92 & cintura1_v66 <= cintura1_v00 * 0.95 ~ "Waist success",
peso1_v66 > peso1_v00 * 0.92 & cintura1_v66 > cintura1_v00 * 0.95 ~ "Fail",
TRUE ~ NA_character_
))
``````

Use `dput(head(your_data))` and paste it in your reply in code format.
A bunch of nested `ifelse()` statements could work as well as it is vectorized, but the code would be harder to read, so yes, `case_when()` is the best choice in your case.