【问题标题】:join two lists of data frames into a single list of binded_rows data frames将两个数据帧列表连接到一个 binded_rows 数据帧列表中
【发布时间】:2019-01-14 19:24:46
【问题描述】:

我有两个数据框列表。我想将每个列表中的每个数据框组合在一起,一个在另一个之上,使用类似 bind_rows 或只是 rbind 之类的东西。这两个列表的列具有完全相同的名称和顺序。

combined <- map_df(rapheys_df_list, XGB_models_Prep, bind_rows) 之类的内容会导致“错误:索引 1 的长度必须为 1”。

如何将两个数据框列表合并为一个组合的单个列表,其中一个列表中的每个数据框都按行组合在另一个列表中?

【问题讨论】:

  • 你需要map2,因为有两个列表,即map2_dfr(rapheys_df_list, XGB_models_Prep, bind_rows)
  • 成功了,谢谢 akrun

标签: r


【解决方案1】:

我们需要map2来绑定两个对应的lists

library(purrr)
map2_dfr(rapheys_df_list, XGB_models_Prep, bind_rows)

数据

rapheys_df_list <- list(data.frame(col1 = 1:3, col2 = 4:6), 
                   data.frame(col1 = 7:9, col2 = 10:12))
XGB_models_Prep <- list(data.frame(col1 = 2:5, col2 = 3:6),
                     data.frame(col1 = 4:6, col2 = 0:2))

【讨论】:

    【解决方案2】:

    基础R

     Reduce(rbind, Map(rbind, rapheys_df_list, XGB_models_Prep))
     # or, with the same result:
     do.call(rbind, Map(rbind, rapheys_df_list, XGB_models_Prep))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-08-14
      • 2020-07-10
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 1970-01-01
      相关资源
      最近更新 更多