Search code examples
rrbind

R merge two dataframes by rows with some similar but also different columns


aa <- data.frame(a= c(1,1,2,3,4), b = c(5,3,2,6,1))
bb <- data.frame(a= c(1,1,6,7,4), c = c(4,3,7,2,1))

I want to merge aa and bb by the rows, however, when I use the rbind() function, it gives me an error that "the numbers of columns of arguments do not match". The final format that I want keeps all the columns that are present in both data frame and fill them with zeros if one column does not exist in the other dataframe's part. A sample output for the reproducible data would be as follows:

enter image description here

Thank you for your time!


Solution

  • You can use bind_rows from dplyr, it's similar to rbind but any missing columns are filled with NA instead of throwing an error.

    cc <- dplyr::bind_rows(aa, bb)
    cc <- replace(cc, is.na(cc), 0)
    
       a b c
    1  1 5 0
    2  1 3 0
    3  2 2 0
    4  3 6 0
    5  4 1 0
    6  1 0 4
    7  1 0 3
    8  6 0 7
    9  7 0 2
    10 4 0 1