Match Merge in R

I'm working on migrating some SAS programs into R, and I'm having a difficult time figuring out the best way to accomplish this seemingly simple bit of logic.

DATA HAVE;
MERGE HAVE1 HAVE2;
BY ID;
RUN;

Example for match merge is,

HAVE1:

|ID|A|B|
|10|1|2|
|20|3|4|
|30|5|6|

HAVE2:

|ID|C|
|10|0|
|30|1|
|40|1|

HAVE:

|ID|A|B|C|
|10|1|2|0|
|20|3|4|.|
|30|5|6|1|
|40|.|.|1|

This would be equivalent to dplyr::full_join.

library(dplyr)

have1 <- data.frame(
    'id' = c(10, 20, 30),
    'a' = c(1, 3, 5),
    'b' = c(2, 4, 6)
)

have2 <- data.frame(
    'id' = c(10, 30, 40),
    'c' = c(0, 1, 1)
)

have <- full_join(
    have1,
    have2,
    by = 'id'
)

have
#>   id  a  b  c
#> 1 10  1  2  0
#> 2 20  3  4 NA
#> 3 30  5  6  1
#> 4 40 NA NA  1

Created on 2022-05-13 by the reprex package (v1.0.0)

Or, if you are feeling more SAS like, you can use merge from base R:


have <- merge(
    have1,
    have2,
    by = 'id',
    all = TRUE
)
have
#>   id  a  b  c
#> 1 10  1  2  0
#> 2 20  3  4 NA
#> 3 30  5  6  1
#> 4 40 NA NA  1

Created on 2022-05-13 by the reprex package (v1.0.0)

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.