0

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 (i.e. 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!

3
  • maybe something like transpose(list(dataset1, dataset2, dataset3)) %>% map(bind_rows))? Or if you just want everything together in one map(map(list(dataset1, dataset2, dataset3), bind_rows), bind_rows). Example data is really required for us to tell you why with confidence. Commented Jun 12, 2018 at 2:02
  • 1
    Hi @zacdav I was looking to retain as one list of separate dataframes after combining them. So the first code worked well. Thank you so much for your help! Commented Jun 12, 2018 at 2:11
  • I'll post it as a proper answer then. Commented Jun 12, 2018 at 2:12

2 Answers 2

1

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)
Sign up to request clarification or add additional context in comments.

Comments

0

We could use

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

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.