0

I like to use randomforest in R, but i get a category with more than 100 levels, so, i can't use randomforest directly. I like to change the variable with the categories into a matrix with binary values (con column for category an values true/false). Is there any function to do this directly?

Original column
1
2
3
4
4

Target columns
1  2  3  4  
1  0  0  0
0  1  0  0
0  0  1  0
0  0  0  1
0  0  0  1

Kind regards.

elcortegano
  • 2,444
  • 11
  • 40
  • 58
Javier
  • 1

1 Answers1

1

An option would be table

table(seq_along(v1), v1)

It does work even if it is an integer/numeric column

data

v1 <- <- c(1, 2, 3, 4, 4)
Community
  • 1
  • 1
akrun
  • 874,273
  • 37
  • 540
  • 662
  • Finaly I has been solved with the library caret like this: library(caret) dmy <- dummyVars(" ~ .", data = ) <- data.frame(predict(dmy, newdata = )) – Javier Aug 01 '18 at 06:43