0

I am pretty new to R, so apologies in advance if this is a simple question.

I have made a Decision Tree classification model using the rpart package.

This works fine and I have used it to predict the variable in my test data. I am trying to make a confusion matrix table to compare the results but I keep getting the following error:

Error in table(EmployeeTest$Leaver, pred) : 
all arguments must have the same length

I have looked online and can't seem to find the solution.

The code I am using to create the matrix is as follows:

table_mat <- table(EmployeeTest$Leaver,pred)

pred is the predicted results of the model and looks like this:

summary(pred)

         pred   
 Leaver    :61  
 Non-Leaver:91 

The Employee Test table contains over 100 variables but the field I am interested in looks like:

summary(EmployeeTest$Leaver)

    Leaver Non-Leaver 
    66         86

Any help would really be appreciated as I have no idea how to fix this. I know the error suggests it is to do with length but both tables contains the same variables with the same lengths.

Thanks in advance.

OzanStats
  • 2,756
  • 1
  • 13
  • 26
Amy
  • 591
  • 3
  • 10
  • 23
  • 1
    Please give a [mcve]. Reading [How to make a great R repoducible example](https://stackoverflow.com/q/5963269/4996248) might help. – John Coleman Jul 28 '18 at 15:19

1 Answers1

0

Problem Solved: if I changed

table_mat <- table(EmployeeTest$Leaver,pred)

to

table_mat <- table(EmployeeTest$Leaver,pred$pred)

it some how fixed the error, despite there only being one field in the pred table

Amy
  • 591
  • 3
  • 10
  • 23