I have a data table with a column containing hexadecimal data; which I would like to covert into binary and create new columns with the binary data. Example code,
library(data.table)
library(BMS)
# Create a data table
dt <- data.table(Z=c(1:4),
HDATA=c("1234","5678","9ACB","DEF0"))
# Convert the HDATA column to binary
Bin_names <- sapply(c(15:0), function(x) paste0('C',x))
dt[,Bin_names:=hex2bin(as.character(HDATA)),]
However this gives me the following error message,
Warning message:
In [.data.table(dt, , :=(Bin_names, hex2bin(as.character(HDATA))), :
Supplied 76 items to be assigned to 4 items of column 'Bin_names' (72 unused)
and the modified data table looks like this,
> dt
Z HDATA Bin_names
1: 1 1234 0
2: 2 5678 0
3: 3 9ACB 0
4: 4 DEF0 1
>
How do I get this to give me an output that looks like this,
Z HDATA C15 C14 C13 C12 C11 C10 C9 C8 C7 C6 C5 C4 C3 C2 C1 C0
1: 1 1234 0 0 0 1 0 0 1 0 0 0 1 1 0 1 0 0
2: 2 5678 0 1 0 1 0 0 1 1 0 1 1 1 1 0 0 0
3: 3 9ABC 1 0 0 1 1 0 1 0 1 0 1 1 1 1 0 0
4: 4 DEF0 1 1 0 1 1 1 1 0 1 1 1 1 0 0 0 0
My actual data table has about 10M rows so I am looking for a fast method to do this. Thanks,