Convert Boolean indicator columns to a single factor column

后端 未结 1 489
悲哀的现实
悲哀的现实 2021-01-19 09:16

A similar questions was asked a few years ago here.

My setting is a little different. My indicator variables are not \"true\" dummy variables because they overlap. <

相关标签:
1条回答
  • 2021-01-19 09:35

    Here's one way using tidyverse functions

    library(tibble)
    library(dplyr)
    library(tidyr)
    dat %>% 
      rowid_to_column() %>% # keep data for each row together
      gather("col", "val", -rowid) %>% 
      mutate(rowid=factor(rowid)) %>% 
      filter(val==1) %>% 
      group_by(rowid) %>% 
      summarize(desired=paste(col, collapse=",")) %>%  #collapse values
      complete(rowid, fill = list(desired="none")) # add "none" for empty groups
    
    #   rowid desired
    #   <fct> <chr>  
    # 1 1     none   
    # 2 2     a      
    # 3 3     a,b,c  
    

    The basic idea involves reshaping the data so we can run functions of groups rather than running functions over rows of a data.frame which isn't as easy.

    0 讨论(0)
提交回复
热议问题