-2

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

  • 4
    It's easier to help you if you include a simple [reproducible example](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) with sample input and desired output that can be used to test and verify possible solutions. – MrFlick Feb 23 '22 at 21:45
  • Thank you for your help, I found the issue, it was simply a display problem... – user18293398 Feb 24 '22 at 09:55

2 Answers2

0

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.

Ruam Pimentel
  • 1,288
  • 4
  • 16
  • Thank you for your help. I am confronted to the same problem with the full_join function. If it may help, it is always the first dataframe appearing in the argument (df1 in you example) that "keeps" its columns in the resulting joined dataframe, and the second dataframe in the argument loses its specific columns – user18293398 Feb 23 '22 at 22:40
  • Please, submit and minimal example as I mentioned above. That will make easier for us to help you. – Ruam Pimentel Feb 24 '22 at 00:56
  • 1
    Thank you for your help, I found the issue, it was simply a display problem... – user18293398 Feb 24 '22 at 09:55
0

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    
AndrewGB
  • 16,126
  • 5
  • 18
  • 49
  • Thank you for your help. I am confronted to the same problem with the bind_rows function (just like in the precedent comment). If it may help, it is always the first dataframe appearing in the argument ('one' in you example) that "keeps" its columns in the resulting joined dataframe, and the second dataframe ('two' in your example) in the argument loses its specific columns – user18293398 Feb 23 '22 at 22:42
  • @user18293398 It's hard to know what the specific issue is without some of your data. You can see that `bind_rows` returns all columns. If you look at `?bind_rows`, then it says, "The output of bind_rows() will contain a column if that column appears in any of the inputs." If you edit your question and provide some of your data, then we can figure out the issue. Just type `dput(head(df1))` into the console, then paste the results into your question (do the same for the second dataframe). Also, include the code you tried too. Then, we can troubleshoot. – AndrewGB Feb 23 '22 at 22:49
  • 1
    Thank you for your help, I found the issue, it was simply a display problem... – user18293398 Feb 24 '22 at 09:55