Creating categorical variables from mutually exclusive dummy variables

前端 未结 3 1452
忘了有多久
忘了有多久 2021-01-11 23:51

My question regards an elaboration on a previously answered question about combining multiple dummy variables into a single categorical variable.

In the question p

3条回答
  •  甜味超标
    2021-01-12 00:21

    Update (2019): Please use dplyr::coalesce(), it works pretty much the same.

    My R package has a convenience function that allows to choose the first non-NA value for each element in a list of vectors:

    #library(devtools)
    #install_github('kimisc', 'muelleki')
    library(kimisc)
    
    df$factor1 <- with(df, coalesce.na(conditionA, conditionB))
    

    (I'm not sure if this works if conditionA and conditionB are factors. Convert them to numerics before using as.numeric(as.character(...)) if necessary.)

    Otherwise, you could give interaction a try, combined with recoding of the levels of the resulting factor -- but to me it looks like you're more interested in the first solution:

    df$conditionAB <- with(df, interaction(coalesce.na(conditionA, 0), 
                                           coalesce.na(conditionB, 0)))
    levels(df$conditionAB) <- c('A', 'B')
    

提交回复
热议问题