如何处理missForest中的不兼容类型错误?

如何处理missForest中的不兼容类型错误?,r,R,我试图使用库missForest的函数missForest(),但总是收到相同的错误消息 代码如下: 图书馆: library(dplyr) library(naniar) library(missForest) 数据: 然后我应用missfreest来摆脱NA: crx <- crx %>% replace_with_na_all(condition = ~.x == "?") crx <- missForest(crx) str显示我们有字符列,其

我试图使用库
missForest
的函数
missForest()
,但总是收到相同的错误消息

代码如下:

图书馆:

library(dplyr)
library(naniar)
library(missForest)
数据:

然后我应用
missfreest
来摆脱
NA

crx <- crx %>% replace_with_na_all(condition = ~.x == "?")
crx <- missForest(crx)

str
显示我们有字符列,其中
missfreest
喜欢元素。我们可以通过
type.convert
as.is=FALSE
追踪数据帧,以便轻松地将字符转换为因子

url <- 'https://archive.ics.uci.edu/ml/machine-learning-databases/credit-screening/crx.data'
crx <- read.csv(url, sep=",", header=FALSE)

crx[crx == "?"] <- NA  ## no `naniar` package needed...
str(crx)
# 'data.frame': 690 obs. of  16 variables:
#  $ V1 : chr  "b" "a" "a" "b" ...
#  $ V2 : chr  "30.83" "58.67" "24.50" "27.83" ...
#  $ V3 : num  0 4.46 0.5 1.54 5.62 ...
#  $ V4 : chr  "u" "u" "u" "u" ...
#  $ V5 : chr  ...
#  ...

crx <- type.convert(crx, as.is=FALSE)
str(crx)
# 'data.frame': 690 obs. of  16 variables:
#  $ V1 : Factor w/ 2 levels "a","b": 2 1 1 2 2 2 2 1 2 2 ...
#  $ V2 : num  30.8 58.7 24.5 27.8 20.2 ...
#  $ V3 : num  0 4.46 0.5 1.54 5.62 ...
#  $ V4 : Factor w/ 3 levels "l","u","y": 2 2 2 2 2 2 2 2 3 3 ...
#  $ V5 : Factor ...
#  ...
url
Error: Assigned data `mean(xmis[, t.co], na.rm = TRUE)` must be 

compatible with existing data. ℹ Error occurred for column `V1`. x Can't convert <double> to <character>. Run `rlang::last_error()` to see where the error occurred.
20.
stop(fallback)
19.
signal_abort(cnd)
18.
cnd_signal(error_assign_incompatible_type(x, value, j, value_arg, cnd_message(cnd)))
17.
(function (cnd) { cnd_signal(error_assign_incompatible_type(x, value, j, value_arg, cnd_message(cnd))) ...
16.
signalCondition(cnd)
15.
signal_abort(cnd)
14.
abort(message, class = c(class, "vctrs_error"), ...)
13.
stop_vctrs(message, class = c(class, "vctrs_error_incompatible"), x = x, y = y, details = details, ...)
12.
stop_incompatible(x, y, x_arg = x_arg, y_arg = y_arg, details = details, ..., message = message, class = c(class, "vctrs_error_incompatible_type"))
11.
stop_incompatible_type(x = x, y = to, ..., x_arg = x_arg, y_arg = to_arg, action = "convert", details = details, message = message, class = class)
10.
stop_incompatible_cast(x, to, x_arg = x_arg, to_arg = to_arg, `vctrs:::from_dispatch` = match_from_dispatch(...))
9.
vec_default_cast(x = x, to = to, x_arg = x_arg, to_arg = to_arg, `vctrs:::from_dispatch` = `vctrs:::from_dispatch`, `vctrs:::df_fallback` = `vctrs:::df_fallback`, `vctrs:::s3_fallback` = `vctrs:::s3_fallback`)
8.
(function () vec_default_cast(x = x, to = to, x_arg = x_arg, to_arg = to_arg, `vctrs:::from_dispatch` = `vctrs:::from_dispatch`, `vctrs:::df_fallback` = `vctrs:::df_fallback`, `vctrs:::s3_fallback` = `vctrs:::s3_fallback`))()
7.
`vec_slice<-`(`*tmp*`, i, value = value[[j]])
6.
withCallingHandlers(for (j in seq_along(x)) { xj <- x[[j]] vec_slice(xj, i) <- value[[j]] x[[j]] <- xj ...
5.
tbl_subassign_row(xj, i, value, value_arg)
4.
tbl_subassign(x, i, j, value, i_arg, j_arg, substitute(value))
3.
`[<-.tbl_df`(`*tmp*`, is.na(xmis[, t.co]), t.co, value = NA_real_)
2.
`[<-`(`*tmp*`, is.na(xmis[, t.co]), t.co, value = NA_real_)
1.
missForest(crx)
Show in New WindowClear OutputExpand/Collapse Output
tibble [690 × 16] (S3: tbl_df/tbl/data.frame)
 $ V1 : chr [1:690] "b" "a" "a" "b" ...
 $ V2 : chr [1:690] "30.83" "58.67" "24.50" "27.83" ...
 $ V3 : num [1:690] 0 4.46 0.5 1.54 5.62 ...
 $ V4 : chr [1:690] "u" "u" "u" "u" ...
 $ V5 : chr [1:690] "g" "g" "g" "g" ...
 $ V6 : chr [1:690] "w" "q" "q" "w" ...
 $ V7 : chr [1:690] "v" "h" "h" "v" ...
 $ V8 : num [1:690] 1.25 3.04 1.5 3.75 1.71 ...
 $ V9 : chr [1:690] "t" "t" "t" "t" ...
 $ V10: chr [1:690] "t" "t" "f" "t" ...
 $ V11: int [1:690] 1 6 0 5 0 0 0 0 0 0 ...
 $ V12: chr [1:690] "f" "f" "f" "t" ...
 $ V13: chr [1:690] "g" "g" "g" "g" ...
 $ V14: chr [1:690] "00202" "00043" "00280" "00100" ...
 $ V15: int [1:690] 0 560 824 3 0 0 31285 1349 314 1442 ...
 $ V16: chr [1:690] "+" "+" "+" "+" ...
argument is not numeric or logical: returning NAargument is not numeric or logical:
returning NAargument is not numeric or logical: returning NAargument is not numeric or 
logical: returning NAargument is not numeric or logical: returning NAargument is not numeric 
or logical: returning NAargument is not numeric or logical: returning NAargument is not 
numeric or logical: returning NAargument is not numeric or logical: returning NAargument is 
not numeric or logical: returning NAargument is not numeric or logical: returning NAargument
is not numeric or logical: returning NA  missForest iteration 1 in progress...done!
Error in FUN(left, right) : non-numeric argument to binary operator
url <- 'https://archive.ics.uci.edu/ml/machine-learning-databases/credit-screening/crx.data'
crx <- read.csv(url, sep=",", header=FALSE)

crx[crx == "?"] <- NA  ## no `naniar` package needed...
str(crx)
# 'data.frame': 690 obs. of  16 variables:
#  $ V1 : chr  "b" "a" "a" "b" ...
#  $ V2 : chr  "30.83" "58.67" "24.50" "27.83" ...
#  $ V3 : num  0 4.46 0.5 1.54 5.62 ...
#  $ V4 : chr  "u" "u" "u" "u" ...
#  $ V5 : chr  ...
#  ...

crx <- type.convert(crx, as.is=FALSE)
str(crx)
# 'data.frame': 690 obs. of  16 variables:
#  $ V1 : Factor w/ 2 levels "a","b": 2 1 1 2 2 2 2 1 2 2 ...
#  $ V2 : num  30.8 58.7 24.5 27.8 20.2 ...
#  $ V3 : num  0 4.46 0.5 1.54 5.62 ...
#  $ V4 : Factor w/ 3 levels "l","u","y": 2 2 2 2 2 2 2 2 3 3 ...
#  $ V5 : Factor ...
#  ...
library(missForest)
crx.i <- missForest(as.data.frame(crx))  ## as.data.frame for safety if you use tidy stuff
# missForest iteration 1 in progress...done!
# missForest iteration 2 in progress...done!
# missForest iteration 3 in progress...done!
# missForest iteration 4 in progress...done!
# missForest iteration 5 in progress...done!