I want to imputate missing values using preProcess. The code is simple. However, a warning occurred and it seems I cannot use knnImpute. I failed to find a correct explanation online.
library(mlbench)
data(Soybean)
library(caret)
imputationObj <- preProcess(Soybean, method = c('knnImpute'), na.remove = FALSE)
imputationObj
imputedSoybean <- predict(imputationObj, Soybean)
summary(imputedSoybean)
> Warning in pre_process_options(method, column_types) :
> The following pre-processing methods were eliminated: 'knnImpute', 'center', 'scale'
The problem is that the data frame Soybean
contains factors. From ?preProcess
, the first argument should be
x a matrix or data frame. Non-numeric predictors are allowed but will be ignored.
but
R> class(Soybean[,2])
[1] "factor"
To put it another way, how would you centre a factor containing the value red,
blueand
green`.