Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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?

like image 715
Doug Fir Avatar asked Oct 27 '25 06:10

Doug Fir


2 Answers

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))
like image 184
akrun Avatar answered Oct 28 '25 22:10

akrun


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))
like image 45
lebatsnok Avatar answered Oct 28 '25 21:10

lebatsnok



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!