Home > other >  Columns disappearing when I use rbind.fill or bind_rows
Columns disappearing when I use rbind.fill or bind_rows

Time:02-24

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

CodePudding user response:

Since you didn't provide a minimal example, it is hard for me to solve exactly your problem. But see below a code that you can reorganize for your data.

Use full_join from the package called dplyr. See instructions for full_join here.

install.packages("dplyr")
library(dplyr)

new_df <- full_join(df1, df2)

df1 is the name first dataset, df2 is the name of the second dataset, and new_df is the name of the joined dataset.

CodePudding user response:

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    
  • Related