Entering edit mode
2.5 years ago
Hi everybody I need trasforms a data frame in a data binarize, for example:
this is my data frame
mydata():
ID AA AB AC AD
Camp1 0 0 2 9
Camp2 2 1 2 9
Camp3 1 1 2 9
Camp4 1 1 2 9
Camp5 0 9 2 2
Camp6 9 9 0 2
Camp7 9 9 0 2
Camp8 1 9 0 2
this is what I would like to achieve: I
D AA_0 AA_1 AA_2 AA_9 AB_0 AB_1 AB_9 AC_2 AC_0 AD_9 AD_2
Camp1 1 0 0 0 1 0 0 1 0 1 0
Camp2 0 0 1 0 0 1 0 1 0 1 0
Camp3 0 1 0 0 0 1 0 1 0 1 0
Camp4 0 1 0 0 0 1 0 1 0 1 0
Camp5 1 0 0 0 0 0 1 1 0 0 1
Camp6 0 0 0 1 0 0 1 0 1 0 1
Camp7 0 0 0 1 0 0 1 0 1 0 1
Camp8 0 1 0 0 0 0 1 0 1 0 1
I have tried to use these two commands:
data <- mydata[,-c(1)]
data <- data %>% dplyr::mutate_if(is.character,as.factor)
data <- mltools::one_hot(data.table::as.data.table(data)
the commands run but the file is not binarized!! Does anyone know an alternative to this?
thank you
If you want to speed up the computation you can just do
(data > 0) * 1
.I think that is not what I wants. It seems they, for every column want to create a new column that says how many 0's, 1's, 2's are in the original column.
yes, that's it!