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,009
  • 10
  • 36
  • 50
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
  • 789,025
  • 32
  • 460
  • 575