Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
menu search
person
Welcome To Ask or Share your Answers For Others

Categories

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
418 views
Welcome To Ask or Share your Answers For Others

1 Answer

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share

548k questions

547k answers

4 comments

86.3k users

...