简体   繁体   中英

looping rbind for lists of dataframes

I have 3 lists (dataset1, dataset2, dataset3) and each of the list contains multiple dataframes which are different. However, in each of the list, the order of the dataframes is the same (ie the first dataframe in list 1, list 2 and list 3 have the same headers, and similarly for the second, third fourth dataframes etc.)

I was thinking of creating a loop to combine the 3 lists of dataframes into 1 list of dataframes. Since each of the dataframes are in the same order in all lists, I tried using the following code:

library(purr)
combined_data <- pmap(dataset1,
                      dataset2,
                      dataset3,
                      ~rbind(..1,..2,..3))

Instead, R returned an error "Error: Element 1 has length 51, not 1 or 90."

I did a check and the first dataframe in all 3 datasets are all "List of 51", while the third dataframe in all 3 datasets are all "List of 90".

However, when i ran the following code:

combined_data <- map2(dataset1,
                      dataset2,
                      ~rbind(.x,.y))
combined_data <- map2(combined_data,
                      dataset3,
                      ~rbind(.x,.y))

It works exactly the way I wanted it to. Hence, I would like to learn from my mistake whether there is anything wrong with my code when I used pmap, especially since I will need to frequently do it on multiple list in the future, and using map2 won't be that sustainable. Thank you all!

You can use purrr::transpose to switch the levels of your list. This would have the effect of placing the first data.frame in each list into their own list, the second data.frame in each list into its own list and so on.

Then you can simply use purrr::map and dplyr::bind_rows to join the data.frames of the new list.

library(tidyverse)
transpose(list(dataset1, dataset2, dataset3)) %>%
   map(bind_rows)

We could use

pmap(list(dataset1, dataset2, dataset3), ~ rbind(...) %>%
                                               as.data.frame)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM