Home > Net >  Merging and combining columns and rows in r
Merging and combining columns and rows in r

Time:03-12

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

CodePudding user response:

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"
)
  •  Tags:  
  • r
  • Related