3

I am looking for a way to bind together the columns of several dataframes even if there is a mismatch in the number of rows. I have tried utilizing "cbind" and "merge" to get my data in a wide format as opposed to dplyr's preference to tall data.

For a simple example, lets say I have 2 dataframes: 1 with 4 rows, one with 5. I want to bind on "Team", and any time there is no match, fill it with either a blank or NA.

Example dataframes:

df1

Team Season Pts
STL 2019 99
CHI 2019 84
DET 2019 74
NYR 2019 78

df2

Team Season Pts
STL 2018 94
CHI 2018 76
MIN 2018 101
DET 2018 73
BOS 2018 112

Desired output would be something like:

Team Season Points Team.1 Season.1 Points.1
STL 2019 99 STL 2018 94
CHI 2019 84 CHI 2018 76
DET 2019 74 DET 2018 73
NYR 2019 78 NA NA NA
MIN 2018 101 NA NA NA
BOS NA NA BOS 2019 112 
bodega18
  • 596
  • 2
  • 13
  • 3
    Does this answer your question? [How to join (merge) data frames (inner, outer, left, right)](https://stackoverflow.com/questions/1299871/how-to-join-merge-data-frames-inner-outer-left-right) – arg0naut91 Mar 13 '20 at 18:53

1 Answers1

3

It is very easy using set operations with dplyr. Specifically you're looking for full_join.

This function has 3 arguments, #1 & #2 are the dataframes you're looking to join. #3 is the "key" argument which tells the funcion by which column to join the data frames. In this case key = 'Team'.

Wai Ha Lee
  • 8,598
  • 83
  • 57
  • 92