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_
  ))

You can read further here
I couldn't test it since I don't know what your data looks like, but if it doesn't work and you need further help, please post a sample of your data.
Use dput(head(your_data)) and paste it in your reply in code format.

It works this way. Thanks

I've tried sth similar before with case_when but it didn't work though.
I guess ifelse will be substituted by case_when

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.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.