I have a problem understanding the behavior of rpart function of R.

Here is the r code part :




attr(ptitanic$age,"class") <- NULL;


#This is a function which draws the tree 



   ptitanicTree  = rpart(survived~., data = data, 
                        control = rpart.control(minsplit = 5, cp = 0), 
                        method = "class")       

   ptitanicOptimal = prune(ptitanicTree, 
                           cp = ptitanicTree$cptable[which.min(ptitanicTree$cptable[,4]),1] )




# I change the position switching column 5 and 6

# Here i get an another tree : why ?

Is anyone able to explain why we get two different trees we have only changed the position of columns in data input ?

First Tree Second Tree

  • $\begingroup$ You did not provide a reproducible example, there is no ptitanic data in base R, nor in rpart package. As about the results, they are nearly the same -- the only difference is that pruning returned one extra level. $\endgroup$
    – Tim
    Jun 16, 2017 at 14:24
  • $\begingroup$ @tim : thank's I have edited the post, the data frame is in the R package rpart.plot (also used to plot the trees). Best regards $\endgroup$
    – TheBridge
    Jun 16, 2017 at 14:35
  • $\begingroup$ By the way the parameter "cp" seems involved in this curious behavior but I can't explain how. regards $\endgroup$
    – TheBridge
    Jun 16, 2017 at 14:38
  • $\begingroup$ Please indent your code properly when asking others to read it. $\endgroup$ Jun 16, 2017 at 15:19
  • $\begingroup$ You might get more responses if your change your title. Your title Understanding rpart package is too vague. A better title would be something like Why does rpart generate a different tree when the order of the variables in the data is changed?. $\endgroup$ Aug 10, 2018 at 2:02

1 Answer 1


The rpart algorithm can generate different trees when the order of the variables is changed because it's a greedy algorithm. At each step when when growing the tree, it may so happen that the optimum Gini Impurity may be achieved by more than one potential split-variable -- which of those variables is actually selected by the algorithm will depend on the order in which the algorithm scans the variables.

  • $\begingroup$ Thanks for changing the title to be more informative for others who have the same question. $\endgroup$ Aug 10, 2018 at 17:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.