I am facing two problems while using caret package in R. I am reproducing an example below:


data(Sonar)   #Random data, just for illustration purpose
Sonar= Sonar[, 1:6] #Selected first 6 columsn only for showing an example. I am assuming V6 to be response.
inTraining <- createDataPartition(Sonar$V6, p = 0.75, list = FALSE)
training <- Sonar[inTraining, ]
testing <- Sonar[-inTraining, ]
modelFit <- train( V6~.,data=training, method="rpart" )  

a. How to extract top three (3) variables from varImp output? I tried to order the variables but for any reason, its not working for me.

b. Also, why the following code doesn't work for "randomForest"?

modelFit <- train( V6~.,data=training, method="rf" )  

> varImp(modelFit)

 Rerun with Debug
 Error in varImp[, "%IncMSE"] : subscript out of bounds 

What is the issue with #1? It runs fine for me and the result of the call to varImp() produces the following, ordered most to least important:

> varImp(modelFit)
rpart variable importance

V5 100.000
V4  38.390
V3  38.362
V2   5.581
V1   0.000

EDIT Based on Question clarification:

I am sure there are better ways, but here is how I might do it:


Regarding #2, you need to add importance=TRUE in order to tell randomForest to calculate them.

> modelFit <- train( V6~.,data=training, method="rf" ,importance = TRUE)
> varImp(modelFit)
rf variable importance

V5 100.000
V3  22.746
V2  21.136
V4   3.797
V1   0.000
| cite | improve this answer | |
  • $\begingroup$ I just need to extract the top three variables. In this example, V5, V4 and V3 $\endgroup$ – learner Aug 28 '14 at 19:40
  • $\begingroup$ Edited response $\endgroup$ – B_Miner Aug 28 '14 at 20:10
  • $\begingroup$ Note varImp(modelFit) won't work if you want to export directly to file. You'll need to convert to a dataframe and add the variable names like either above or Scott's dplyr answer below $\endgroup$ – Anna Jul 31 '18 at 16:16

Below is a dplyr option using pipes. It does seems strange that this is so hard to access.


col_index <- varImp(modelFit)$importance %>% 
  mutate(names=row.names(.)) %>%

If you want the top three, then this should work:

imp_names <- col_index$names[1:3]

You can also subset the original data frame like this:

Sonar_imp <- Sonar[,imp_names]
| cite | improve this answer | |

This should do the trick:

| cite | improve this answer | |
  • $\begingroup$ without sorting, this just gives the first three variable namesn V1, V2, V3 $\endgroup$ – vinnief Jun 12 '16 at 9:02

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