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

Merging and combining columns and rows in r

I have two data sets with many columns called the same and others differently, about 350 columns. How I can combine all this data?

combination of df1 and df2 to create df3

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 :

We could use bind_rows in this case:

library(dplyr)

bind_rows(df1, df2)
     x1    x2 x3     x4   
  <dbl> <dbl> <chr>  <chr>
1    10    40 male   NA   
2    20    50 male   NA   
3    30    60 female NA   
4    50    NA female b    
5    40    NA male   a    
6    30    NA female c   

data

df1 <- tribble(
  ~x1, ~x2, ~x3,
  10, 40, "male",
  20, 50, "male",
  30, 60, "female"
)

df2 <- tribble(
  ~x1, ~x3, ~x4,
  50, "female", "b",
  40, "male", "a", 
  30, "female", "c"
)
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