Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

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!

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>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
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading