在插入符号中使用预测函数时 R 中的警告

A warning in R when using predict function in caret

我想使用预处理来估算缺失值。代码很简单。但是,出现警告,似乎我无法使用 knnImpute。我没能在网上找到正确的解释。

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'

问题是数据框 Soybean 包含因子。从 ?preProcess 开始,第一个参数应该是

x a matrix or data frame. Non-numeric predictors are allowed but will be ignored.

但是

R> class(Soybean[,2])
[1] "factor"

换句话说,您如何将包含值 red,blueandgreen` 的因子居中。