Search code examples
rdataframedplyrplyrrbind

Columns disappearing when I use rbind.fill or bind_rows


I just started using R.

I am trying to merge two dataframes using the rbind.fill or bind_rows function.

The two dataframes share almost all columns, expect 8 of them (some of them in the first dataframe, some in the second one). I cannot find any way to ensure that the new dataframe includes all columns. Would anyone have an idea where the mistake might lie?

Thanks in advance for your precious help


Solution

  • bind_rows will return all columns from both dataframes. For the columns that do not occur in the other dataset, the column will fill with NA.

    library(dplyr)
    
    one <- starwars[1:4, c(1,3,5,6,7)]
    two <- starwars[9:12, c(1,2,4,6)]
    
    bind_rows(one, two)
    
      name               mass skin_color  eye_color birth_year height hair_color   
      <chr>             <dbl> <chr>       <chr>          <dbl>  <int> <chr>        
    1 Luke Skywalker       77 fair        blue            19       NA NA           
    2 C-3PO                75 gold        yellow         112       NA NA           
    3 R2-D2                32 white, blue red             33       NA NA           
    4 Darth Vader         136 white       yellow          41.9     NA NA           
    5 Biggs Darklighter    NA NA          brown           NA      183 black        
    6 Obi-Wan Kenobi       NA NA          blue-gray       NA      182 auburn, white
    7 Anakin Skywalker     NA NA          blue            NA      188 blond        
    8 Wilhuff Tarkin       NA NA          blue            NA      180 auburn, grey