Convert a factor column to multiple boolean columns

后端 未结 3 595
一生所求
一生所求 2021-01-18 05:20

Given data that looks like:

library(data.table)
DT <- data.table(x=rep(1:5, 2))

I would like to split this data into 5 boolean columns t

相关标签:
3条回答
  • 2021-01-18 05:54
    library(data.table)
    DT <- data.table(x=rep(1:5, 2))
    
    # add column with id
    DT[, id := seq.int(nrow(DT))]
    
    # cast long table into wide
    DT.wide <- dcast(DT, id ~ x, value.var = "x", fill = 0, fun = function(x) 1)
    
    0 讨论(0)
  • 2021-01-18 06:05

    How about model.matrix?

    model.matrix(~factor(x)-1,data=DT)
    
       factor(x)1 factor(x)2 factor(x)3 factor(x)4 factor(x)5
    1           1          0          0          0          0
    2           0          1          0          0          0
    3           0          0          1          0          0
    4           0          0          0          1          0
    5           0          0          0          0          1
    6           1          0          0          0          0
    7           0          1          0          0          0
    8           0          0          1          0          0
    9           0          0          0          1          0
    10          0          0          0          0          1
    attr(,"assign")
    [1] 1 1 1 1 1
    attr(,"contrasts")
    attr(,"contrasts")$`factor(x)`
    [1] "contr.treatment"
    

    Apparently, you can put model.matrix into [.data.table to give the same results. Not sure if it would be faster:

    DT[,model.matrix(~factor(x)-1)]
    
    0 讨论(0)
  • 2021-01-18 06:08

    There is also nnet::class.ind

    library(nnet)
    
    cbind(DT, setnames(as.data.table(DT[, class.ind(x)]),paste0('col', unique(DT$x))))
    
    0 讨论(0)
提交回复
热议问题