title: How to one hot encode several categorical variables in R
body: <p>I'm working on a prediction problem and I'm building a decision tree in R, I have several categorical variables and I'd like to one-hot encode them consistently in my training and testing set.
  I managed to do it on my training data with : </p>
  
  <pre><code>temps &lt;- X_train
  tt &lt;- subset(temps, select = -output)
  oh &lt;- data.frame(model.matrix(~ . -1, tt), CLASS = temps$output)
  </code></pre>
  
  <p>But I can't find a way to apply the same encoding on my testing set, how can I do that? </p>