One hot encoding error "sort.list(y)..."
I may be missing the point, but you don't need to do this one variable at a time in a loop and then bind the columns together - that's not the R way.  Here's an alternative:

# create some simulated data
n &lt;- 10000
X &lt;- data.frame(f_a_ = factor(sample(1:400, n, replace = TRUE)),
                f_b_ = factor(sample(1:60, n, replace = TRUE)),
                f_c_ = factor(sample(1:6, n, replace = TRUE)),
                f_d_ = factor(sample(1:5, n, replace = TRUE)),
                y = rep(1, n))

# use R's inbuilt understanding of formulas to do the "one hot encoding" for you
x_onehot &lt;- model.matrix(y ~ ., data = X)