Paras Karandikar
Paras Karandikar

Reputation: 61

Faster way to rbind data frames from a list of lists

I have a foreach loop that returns a list of lists. Each sub-list is a list of 3 data frames and I want to rbind them into 3 different data frames. Right now I am doing it like this:

x_final=data.frame
y_final=data.frame()
z_final=data.frame()
for(i in 1:length(big_list))
{
   x=big_list[[i]][1]
   y=big_list[[i]][2]
   z=big_list[[i]][3]
   x_final=rbind(x_final,x)
   y_final=rbind(y_final,y)
   z_final=rbind(z_final,z)
} 

The problem is when length of big_list is large this loop takes a lot of time. Is there any other way of doing it faster? Any sort of help would be highly appreciated.

Upvotes: 2

Views: 453

Answers (1)

akrun
akrun

Reputation: 887158

An option would be to transpose the big_list and use bind_rows

library(dplyr)
library(purrr)
out_lst <- transpose(big_list) %>% 
                map(bind_rows)

Upvotes: 1

Related Questions