Doug Fir
Doug Fir

Reputation: 21232

join two lists of data frames into a single list of binded_rows data frames

I have two lists of dataframes. I would like to combine each dataframe in each of the list, one on top of the other using somehting like bind_rows or just rbind. The two lists have the columns with exact same names and order.

Something like combined <- map_df(rapheys_df_list, XGB_models_Prep, bind_rows) which resulted in "Error: Index 1 must have length 1".

How can I join the two lists of dataframes into a combined single list where each dataframe in one list is combined by rows on top of the other?

Upvotes: 4

Views: 542

Answers (2)

lebatsnok
lebatsnok

Reputation: 6459

base 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))

Upvotes: 2

akrun
akrun

Reputation: 887163

We need map2 for binding two corresponding lists

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

data

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))

Upvotes: 3

Related Questions