I have a large dataset that outlines 5 different medications given to a patient at a single time point, each medication has it's own column and is binary with 0 indicating no medication and 1 indication they had the medication, there are a small number of patients who will have had more than 1 medication. I want to condense these into a single column that outlines which medication the patient got or if they got more than one medication then code this as "multiple". Below is a reprex of a simplified version of what I am trying to do, the column "induction agent" is what I am trying to get. The code below works partially but doesn't allow for creation of a "multiple medications" category and gave me incorrect quantities when I summed the total values in the larger datasheet.
Thanks for your help
id<- c(1,2,3) cd_25 <- c(0,0,1) ivig <- c(1,0,0) b_cell <- c(0,1,0) df <- data.frame(id, cd_25, ivig, b_cell) df$induction_agent <- names(df[2:4])[max.col(df[2:4])] print (df) #> id cd_25 ivig b_cell induction_agent #> 1 1 0 1 0 ivig #> 2 2 0 0 1 b_cell #> 3 3 1 0 0 cd_25