title: R: Error in randomForest.default(m, y, ...) : Can't have empty classes in y
body: <p>Ok,  I am getting the error </p>
  
  <p>"Error in randomForest.default(m, y, ...) : Can't have empty classes in y."</p>
  
  <p>When running Randomforest in my code.</p>
  
  <p>This is the code I am trying to run</p>
  
  <pre><code>set.seed(415)
  
  trainingdata &lt;- data.combined[1:891,]
  testdata &lt;- data.combined[892:1309,]
  
  fitRF &lt;- randomForest(as.factor(Survived) ~ Pclass + avg.fare + new.title + Parch + FamilyID2, 
                        data=trainingdata,
                        importance =T,
                        ntree=2000)
  
  fitRF
  varImpPlot(fitRF)
  
  
  Prediction &lt;- predict(fitRF, testdata)
  submit &lt;- data.frame(PassengerID = testdata$PassengerId, Survived = Prediction)
  write.csv(submit, file="14072017_3_RF.csv", row.names = F)
  </code></pre>
  
  <p>When I run the fitRF &lt;- randomForest line, i get the error.  I have looked at the structures of both my trainingdata and my testdata and they are identical, all variable types match with corresponding levels as below.</p>
  
  <p><a href="https://i.stack.imgur.com/lUuSX.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/lUuSX.png" alt="enter image description here"></a></p>
  
  <p><a href="https://i.stack.imgur.com/zT83h.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/zT83h.png" alt="enter image description here"></a></p>
  
  <p>I just cant figure out whats going on</p>
  
  <p>I read something about it may be to do with a factor levels issue so I then tried running the commands below to create my separated data from my combined data set, </p>
  
  <pre><code>trainingdata &lt;- droplevels(data.combined[1:891,])
  testdata &lt;- droplevels(data.combined[892:1309,])
  </code></pre>
  
  <p>This just resulted in the levels for FamilyID2 not being equal, one was 22 the other was 18.</p>