0

Suppose I have the following data table in R:

DT <- data.table::data.table(y=runif(1e4), x1=rnorm(1e4), x2=as.factor(sample(1:11,1e4,TRUE)))

Since x2 takes value 1 to 11, the maximum binary representation for 11 is 1011, so 4 columns would be enough, I want to convert x2 to 4 binary columns such that:

y           x1        x2   b1  b2  b3  b4
0.17438022  0.1925023  11  1    0  1   1
0.34850700  1.0412363  3   0    0  1   1

How to do it in R?

2
  • This was answered here: stackoverflow.com/questions/12088080/… Commented Oct 16, 2018 at 14:50
  • I'd probably use digitsBase() from the sfsmisc package. Try, for example, t(digitsBase(1:11, 2, ndigits=4)). Commented Oct 16, 2018 at 14:56

1 Answer 1

0
  • For the converting part I've used THIS Answer.

  • As already provided by you. DT must be a data.table.


binarys <-
sapply(
    as.numeric(as.character(DT$x2)), function(x) {
        sub(".*(?=.{4}$)", "",
        paste(rev(as.integer(intToBits(x))), collapse=""),
        perl = T)
    })

DT[,c(c(DT), tstrsplit(binarys,""))]

#            y         x1 x2 V4 V5 V6 V7
#1: 0.09963794  0.2799082  8  1  0  0  0
#2: 0.04547423 -0.8783603 11  1  0  1  1
#3: 0.41489062 -0.8319349 10  1  0  1  0
#4: 0.96606376  0.6323427  3  0  0  1  1
#5: 0.50516936 -0.5751176 11  1  0  1  1

sub(".*(?=.{4}$)" ... is used to remove everything except for the last 4 digits. If your numbers get bigger you might want to tweak this.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.