首页 > 解决方案 > 从 R 中的列表列表中的 imap_dfr 到 bind_rows

问题描述

我正在尝试bind_rowsimap_dfr函数中使用。我有列表列表,因此我想将“1”、“650”和“1300”(都是数据帧)的结果绑定在一起。我正在尝试使用该map_dfr功能,但似乎无法使其正常工作。我还试图创建一个新列“模型”,其中包含它出现的列表列表。

x2 <- imap_dfr(
  shap_values_results[[1]][[1]]$shap_score,
  shap_values_results[[1]][[650]]$shap_score,
  shap_values_results[[1]][[1300]]$shap_score,
  .f = bind_rows,
  .id = "model"
)

我收到这条消息:

错误:参数 1 必须有名称

我哪里错了?我想创建一个数据框,其中每个列表行列表绑定在一起。

如果我使用:

x2 <- bind_rows(
  shap_values_results[[1]][[1]]$shap_score,
  shap_values_results[[1]][[650]]$shap_score,
  shap_values_results[[1]][[1300]]$shap_score
)

我可以获得绑定在一起的列表的数据框,但我缺少/错过了该数据框来自的列表“ID”。

(我正在努力将数据过滤到dput()这里。)

编辑:

列表结构:

> str( shap_values_results[[1]][[1]]$shap_score)
'data.frame':   2190 obs. of  30 variables:
 $ holiday              : num  -0.306 -0.281 -0.254 -0.248 -0.247 ...
 $ temp                 : num  -0.0853 -0.025 -0.0735 0.0177 0.0672 ...
 $ wind                 : num  -0.5213 0.1489 0.605 0.1408 0.0605 ...
 $ humidity             : num  0.11052 0.15571 -0.00248 0.12065 0.17133 ...
 $ barometer            : num  -0.0268 -0.0361 -0.0957 -0.0236 -0.0213 ...
 $ weekday              : num  0.0742 0.0918 0.0357 0.0882 0.0921 ...
 $ weekend              : num  0 0 0 0 0 0 0 0 0 0 ...
 $ workday_on_holiday   : num  -0.000757 -0.000757 -0.003148 -0.000757 -0.000757 ...
 $ weekend_on_holiday   : num  0 0 0 0 0 0 0 0 0 0 ...
 $ protocol_active      : num  0 0 0 0 0 0 0 0 0 0 ...
 $ text_fog             : num  0.02891 0.0357 0.00851 0.03118 0.02916 ...
 $ text_light_rain      : num  0.0392 0.0362 0.0362 0.0396 0.0394 ...
 $ text_mostly_cloudy   : num  -0.00406 -0.00931 -0.00662 -0.02493 -0.01843 ...
 $ text_passing_clouds  : num  -0.001616 -0.001324 -0.012845 -0.000541 -0.001381 ...
 $ text_rain            : num  0.00735 -0.00494 -0.01891 -0.00494 -0.00494 ...
 $ text_scattered_clouds: num  -0.1 -0.123 -0.217 -0.128 -0.107 ...
 $ text_sunny           : num  0.000442 0.000909 0.000277 -0.003121 -0.002015 ...
 $ month_1              : num  -0.0176 0.1267 0.0599 0.1313 0.1148 ...
 $ month_2              : num  0.02293 0.00719 -0.00393 0.09392 0.07848 ...
 $ month_3              : num  -0.023 -0.0329 -0.0335 -0.0163 -0.0073 ...
 $ month_4              : num  -0.1067 -0.1359 -0.1217 -0.126 -0.0669 ...
 $ month_5              : num  -0.0768 -0.1301 -0.2356 -0.1306 -0.0891 ...
 $ month_6              : num  -0.0574 -0.0162 -0.0284 -0.0361 -0.0418 ...
 $ month_7              : num  -0.0787 -0.0724 -0.0537 -0.066 -0.0683 ...
 $ month_8              : num  -0.0587 -0.0948 -0.0845 -0.0947 -0.0894 ...
 $ month_9              : num  0.0445 0.0603 0.0075 0.0609 0.0482 ...
 $ month_10             : num  0.0743 0.065 0.0573 0.0654 0.0741 ...
 $ month_11             : num  0.058 0.0215 0.0164 0.0251 0.0547 ...
 $ month_12             : num  0.165 0.245 0.102 0.19 0.143 ...
 $ BIAS0                : num  0.00241 0.00241 0.00241 0.00241 0.00241 ...

标签: r

解决方案


在这里,我们已经从 the 中提取了 data.framelist并且不需要.f = bind_rowsin imap_dfr,相反它可以只是一个占位符标识 ( I),然后将其包装在 a 中list

out <- imap_dfr(list(`1` = 
      shap_values_results[[1]][[1]]$shap_score,
      `650` = shap_values_results[[1]][[650]]$shap_score,
      `1300` = shap_values_results[[1]][[1300]]$shap_score),
    .f = I,
   .id = "model"
   )

如果我们希望 'model' 列作为索引,则将其放在 a 中list并设置list具有相同值的名称

out <- bind_rows(list(`1` = shap_values_results[[1]][[1]]$shap_score,
      `650` = shap_values_results[[1]][[650]]$shap_score,
      `1300` = shap_values_results[[1]][[1300]]$shap_score), .id = 'model')    

除了上述之外,最好以自动方式执行此操作,即不重复提取

map_dfr(set_names(shap_values_results[[1]][c(1, 650, 1300)], 
     unique(names(shap_values_results[[1]][c(1, 650, 1300)]))), ~ 
            .x$shap_score, .id = "model")

推荐阅读