0

Say I have the following two data-frames and I want to merge them using merge function.

> x <- data.frame(id=c("a", "b", "c"), type=c("good", "ok", "not-ok"))
> y <- data.frame(id=c("b", "d"), type=c("ok", "good"), kind=c("A", "B"))
> merge(x, y, by.x="id", by.y="id", all=TRUE)
  id type.x type.y kind
1  a   good   <NA> <NA>
2  b     ok     ok    A
3  c not-ok   <NA> <NA>
4  d   <NA>   good    B

What I want to get is

  id   type    kind
1  a   good    <NA>
2  b     ok       A
3  c not-ok    <NA>
4  d   good       B

Is there a nice way to do this in R, please? Thanks.

LaTeXFan
  • 1,136
  • 4
  • 14
  • 36

2 Answers2

2

In Base R:

merge(x, y, all = TRUE)

With tidyverse:

First, an addition when you are creating your data frame:

x <- data.frame(id=c("a", "b", "c"), type=c("good", "ok", "not-ok"),
                stringsAsFactors = FALSE)

y <- data.frame(id=c("b", "d"), type=c("ok", "good"), kind=c("A", "B"),
                stringsAsFactors = FALSE)

Add stringsAsFactors = FALSE to prevent Warnings in the next step:

library(tidyverse)

df <- full_join(x,y)

Edited based on @markus' comments.

FMM
  • 1,857
  • 1
  • 15
  • 38
1

Easy!

library(dplyr)

x %>% full_join(y, by  = c("id", "type"))

  id   type    kind
1  a   good    <NA>
2  b     ok       A
3  c not-ok    <NA>
4  d   good       B
Shinobi_Atobe
  • 1,793
  • 1
  • 18
  • 35