title: Efficient way to do one-hot encoding in R on large data
body: <p>I'm trying to create a one-hot representation of my data. This is my approach:</p>
  
  <pre><code>data(iris)
  iris = as.data.frame(apply(iris, 2, function(x) as.factor(x)))
  head(iris)
  
  iris_ohe &lt;- data.frame(model.matrix(~.-1, iris))
  head(iris_ohe)
  dim(iris_ohe)
  </code></pre>
  
  <p>The thing is, the data I'm working on has over 1 million rows, and doing the encoding, I get a matrix with over 100 columns. This is too much for <code>R</code> and I run out of memory:</p>
  
  <pre><code>Error: cannot allocate vector of size 10204.5 Gb
  </code></pre>
  
  <p>Is there a better approach I could try? </p>