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

后端 未结 4 749
滥情空心
滥情空心 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 09:02

    This is yet another use case for merge after suitable transformation.

    library(reshape2)
    f<-with(df,stack(setNames(strsplit(as.character(features),","),id)))
    d<-dcast(f,ind~values,length,value.var="ind")
    out<-merge(df[,1:2],d,by.x="id",by.y="ind")
    
    print(out)
    
      id value A B C D E F
    1  1    25 1 1 0 1 0 1
    2  2    24 0 1 1 0 1 0
    

    This can also be done using only default libraries (without reshape2) in a variety of slightly messier ways. In the above, you can substitute the d and out lines with the following instead:

    d<-xtabs(count~ind+values,transform(f,count=1))
    out<-merge(df[,1:2],as.data.frame.matrix(d),by.x="id",by.y="row.names")
    

提交回复
热议问题