1

I have df1:

ID   Score
1    12
2    14
3    11
4    15
5    16
6    11

I have df2:

ID   Score2
1    124
2    145
3    110
5    161
6    115

I would like to combine the two dfs by ID so that df3 shows a NA for the missing ID #4, like so:

ID   Score Score2
1    12    124
2    14    145
3    11    110
4    15    NA
5    16    161
6    11    115
Evan
  • 1,477
  • 1
  • 17
  • 34

1 Answers1

0

You can use a full_join, which will keep all values from both dataframes.

library(dplyr)

full_join(df1, df2, by = "ID")

Or with base R

merge(df1, df2, by = "ID", all = T)

Output

  ID Score Score2
1  1    12    124
2  2    14    145
3  3    11    110
4  4    15     NA
5  5    16    161
6  6    11    115
AndrewGB
  • 16,126
  • 5
  • 18
  • 49