将多重填补数据集提取为数据框列表

我正在使用mice包对我的数据进行多重填补,并希望将结果提取为一个数据框列表。这是我的尝试,使用了mapmice::complete

library(mice)m = 2data <- airquality[1:30,]imp <- mice(data, seed = 123, m, print = F) #填补数据imp_list_df <- map(1:m, function(x) complete(imp, x))

我希望每个对象的类别如下:

> class(imp_list_df)[1] "list"> class(imp_list_df[1])[1] "data.frame"

目前class(imp_list_df[1])返回的是"list"。有什么建议可以替代map函数,以便返回一个数据框列表吗?


回答:

它是一个list的data.frames。

str(imp_list_df)List of 2 $ :'data.frame':   30 obs. of  6 variables:  ..$ Ozone  : int [1:30] 41 36 12 18 6 28 23 19 8 30 ...  ..$ Solar.R: int [1:30] 190 118 149 313 8 313 299 99 19 194 ...  ..$ Wind   : num [1:30] 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...  ..$ Temp   : int [1:30] 67 72 74 62 56 66 65 59 61 69 ...  ..$ Month  : int [1:30] 5 5 5 5 5 5 5 5 5 5 ...  ..$ Day    : int [1:30] 1 2 3 4 5 6 7 8 9 10 ... $ :'data.frame':   30 obs. of  6 variables:  ..$ Ozone  : int [1:30] 41 36 12 18 18 28 23 19 8 16 ...  ..$ Solar.R: int [1:30] 190 118 149 313 66 307 299 99 19 194 ...  ..$ Wind   : num [1:30] 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...  ..$ Temp   : int [1:30] 67 72 74 62 56 66 65 59 61 69 ...  ..$ Month  : int [1:30] 5 5 5 5 5 5 5 5 5 5 ...  ..$ Day    : int [1:30] 1 2 3 4 5 6 7 8 9 10 ...

正确的提取list的方式是使用[[

class(imp_list_df[[1]])[1] "data.frame"

当我们使用[进行提取时,它仍然返回一个list,即使用[1],它提取为长度为1的list,其中的元素是data.frame

> str(imp_list_df[1])List of 1 $ :'data.frame':   30 obs. of  6 variables:  ..$ Ozone  : int [1:30] 41 36 12 18 6 28 23 19 8 30 ...  ..$ Solar.R: int [1:30] 190 118 149 313 8 313 299 99 19 194 ...  ..$ Wind   : num [1:30] 7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ...  ..$ Temp   : int [1:30] 67 72 74 62 56 66 65 59 61 69 ...  ..$ Month  : int [1:30] 5 5 5 5 5 5 5 5 5 5 ...  ..$ Day    : int [1:30] 1 2 3 4 5 6 7 8 9 10 ...> str(imp_list_df[[1]])'data.frame':   30 obs. of  6 variables: $ Ozone  : int  41 36 12 18 6 28 23 19 8 30 ... $ Solar.R: int  190 118 149 313 8 313 299 99 19 194 ... $ Wind   : num  7.4 8 12.6 11.5 14.3 14.9 8.6 13.8 20.1 8.6 ... $ Temp   : int  67 72 74 62 56 66 65 59 61 69 ... $ Month  : int  5 5 5 5 5 5 5 5 5 5 ... $ Day    : int  1 2 3 4 5 6 7 8 9 10 ...

此外,要一次检查每个元素的class,请使用sapply/lapply

sapply(imp_list_df, class)[1] "data.frame" "data.frame"

Related Posts

使用LSTM在Python中预测未来值

这段代码可以预测指定股票的当前日期之前的值,但不能预测…

如何在gensim的word2vec模型中查找双词组的相似性

我有一个word2vec模型,假设我使用的是googl…

dask_xgboost.predict 可以工作但无法显示 – 数据必须是一维的

我试图使用 XGBoost 创建模型。 看起来我成功地…

ML Tuning – Cross Validation in Spark

我在https://spark.apache.org/…

如何在React JS中使用fetch从REST API获取预测

我正在开发一个应用程序,其中Flask REST AP…

如何分析ML.NET中多类分类预测得分数组?

我在ML.NET中创建了一个多类分类项目。该项目可以对…

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注