title: CART (classification and regression tree) model prediction error message in R says - more items are supplied than to be assigned
body: <p>I got stuck on the CART model in R. My data set has about 7000 rows, 50 columns. I split the dataset to 75% training and 25% testing data, so my training set has 5658 rows and my test set has 1886 rows.</p>
  
  <p>First i created a CART model with a few features in training dataset, which went through and the tree looked all good. Next I tried to apply this model to the testing data, but an error message always showed as below when the very last line of code was run. I have also attached a screenshot.</p>
  
  <pre><code>library(dplyr)
  set.seed(111)
  cart_sample &lt;- sample.int(n = nrow(cart), size = floor(.75*nrow(cart)), replace = F)
  cart_train &lt;- cart[cart_sample, ]
  cart_test  &lt;- cart[-cart_sample, ]
  
  # Build the model1
  # Set cp = 0.01
  library(rpart)
  library(rpart.plot)
  
  model1 &lt;- price ~ neighbourhood_group + property_type + room_type + accommodates + bedrooms + bathrooms + amenities_ac + amenities_petsallowed + amenities_freeparking
  tree1 &lt;- rpart(formula = model1, data = cart_train, control = rpart.control(cp = 0.01))
  rpart.plot(tree1)
  
  # Predict using test dataset
  cart_test$pred1_price &lt;- predict(tree1, data = cart_test)
  </code></pre>
  
  <p>Error in set(x, j = name, value = value) : Supplied 5658 items to be assigned to 1886 items of column 'pred1_price'. If you wish to 'recycle' the RHS please use rep() to make this intent clear to readers of your code.</p>
  
  <p><a href="https://i.stack.imgur.com/SLjqR.png" rel="nofollow noreferrer">code and error message screenshot</a></p>
  
  <p>I don't understand why there is an error. The size and training set and test set does not have to be equal. What am i missing?</p>