R: Converting multiple binary columns into one factor variable whose factors are binary column names

后端 未结 4 1495
挽巷
挽巷 2021-01-03 02:09

I am a new R user. Currently I am working on a dataset wherein I have to transform the multiple binary columns into single factor column

Here is the example:

4条回答
  •  有刺的猬
    2021-01-03 02:13

    Melt is certainly a solution. I'd suggest using the reshape2 melt as follows:

    library(reshape2)
    
    df=data.frame(Property.RealEstate=c(0,0,1,0,0,0),
                  Property.Insurance=c(0,1,0,1,0,0),
                  Property.CarOther=c(0,0,0,0,1,0),
                  Property.Unknown=c(0,0,0,0,0,1))
    
    #add id column (presumably you have ids more meaningful than row numbers)
    df$row=1:nrow(df)
    
    #melt to "long" format
    long=melt(df,id="row")
    
    #only keep 1's
    long=long[which(long$value==1),]
    
    #merge in ids for NA entries
    long=merge(df[,"row",drop=F],long,all.x=T)
    
    #clean up to match example output
    long=long[order(long$row),"variable",drop=F]
    names(long)="Property"
    long$Property=gsub("Property.","",long$Property,fixed=T)
    
    #results
    long
    

提交回复
热议问题