R - Convert various dummy/logical variables into a single categorical variable/factor from their name

后端 未结 3 1103
别那么骄傲
别那么骄傲 2021-02-04 16:28

My question has strong similarities with this one and this other one, but my dataset is a little bit different and I can\'t seem to make those solutions work. Please excuse me i

3条回答
  •  一个人的身影
    2021-02-04 16:56

    library(tidyr)
    library(dplyr)
    
    df <- df %>%
      gather(type, count, -id)
    df <- df[complete.cases(df),][,-3]
    df[order(df$id),]
       id       type
    1   1 conditionA
    7   2 conditionB
    13  3 conditionC
    19  4 conditionD
    5   5 conditionA
    

提交回复
热议问题