Reshaping a data.frame so a column containing multiple features becomes multiple binary columns

后端 未结 4 743
滥情空心
滥情空心 2021-01-27 08:06

I have a dataframe like this

df <-data.frame(id = c(1,2),
                value = c(25,24),
                features = c(\"A,B,D,F\",\"C,B,E\"))

print(df)

i         


        
4条回答
  •  孤独总比滥情好
    2021-01-27 08:50

    A dplyr/tidyr solution

    library(dplyr)
    library(tidyr)
    
    separate(df,features,1:4,",",extra="merge") %>%
      gather(key,letter,-id,-value) %>%
      filter(!is.na(letter)) %>%
      select(-key) %>%
      mutate(n=1) %>%
      spread(letter,n) %>%
      mutate_each(funs(ifelse(is.na(.),0,1)),A:F)
    

提交回复
热议问题