I have a function that queries a database and returns a list of two data frames (df1 and df2). If I lapply iteratively over that function, I return a list of nested lists with the two data frames.
The resultant list is structured as below:
#e.g. sample list of lists of 2 data frames
A1 <- data.frame(Value =c("A","B","C"))
A2 <- data.frame(Value =c("1","2","3"))
B1 <- data.frame(Value =c("D","E","F"))
B2 <- data.frame(Value =c("4","5","6"))
C1 <- data.frame(Value =c("G","H","I"))
C2 <- data.frame(Value =c("7","8","9"))
myList <- list( list(df1 = A1, df2 = A2),
list(df1 = B1, df2 = B2),
list(df1 = C1, df2 = C2))
I then want to combine the data frames into their own separate big data frames - df1_All and df2_All.
How can I extract all of the df1 data frames from the list and combine them into a larger data frame? I am thinking it would be to use make use of a do.call(rbind) construct with an apply or map function applied to myList?
do.call("rbind", lapply(myList, "[[", 1))
anddo.call("rbind", lapply(myList, "[[", 2))
is what you need ? – Ronak Shahpurrr::map_dfr(myList, 'df1')
, or get both the first and second at once withpurrr::pmap(myList, dplyr::bind_rows)
– alistairedo.call(Map, c(rbind, myList))
to put it all back into a length-2 list. – thelatemail