Search code examples
rlistdataframemergecbind

How to add put together a single dataframe from a list of dataframes


I have a list of dataframes that I want to put columns together. To illustrate here's a dummy set:

Data1 <- data.frame(A = c(1, 2, 3, 4, 5),
                    B = c(2, 3, 5, 3, 10))
Data2 <- data.frame(A = c(1, 2, 3, 4, 6), 
                    C = c(3, 4, 8, 12, 2))
Data3 <- data.frame(A = c(1, 2, 3, 4, 6), 
                    D = c(4, 3, 1, 9, 2))
list <- list(Data1, Data2, Data3)

I want the output to look like this:

A  B  C  D
1  2  3  4
2  3  4  3
3  5  8  1
4  3 12  9
5 10 NA NA
6 NA  2  2

My real data has many dataframes inside the list, and I have many lists, so I would like the code to not have to explicitly state the name of the dataframes, which I've been doing using the merge() function.

Thank you!


Solution

  • We can use reduce with full_join

    library(dplyr)
    library(purrr)
    reduce(list, full_join, by = 'A')
    

    If there are many list, place them in all in a list, loop over the list and then use reduce

    map(list(list1, list2, list3, ..., listn), ~ reduce(.x, full_join, by = 'A'))
    

    Placing the list in a list can be automated with mget

    map(mget(ls(pattern = '^list\\d+$')), ~ reduce(.x, full_join, by = 'A'))
    

    Here, we assume the names of the lists as list1, list2, etc.