1

I would like to merge this two dataframes :

library(dplyr)
table1 <- data.frame(
  siren = "A",
  siret = "X",
  var1 = 0
)
table2 <- data.frame(
  siren = c("A","A"),
  siret = c("X","Y")
)
 

Expected result :

result <- data.frame(
  siren = c("A","A",
  siret = c("X","Y"),
  var1 = c(0,NA)
)

Maybe it could be done with a left_join in dplyr ?

Many thanks in advance

Damien Dotta
  • 771
  • 3
  • 13

3 Answers3

1

Try this

full_join(table1, table2)
big parma
  • 337
  • 1
  • 13
1

Almost :) Try a right_join.

    right_answer <- right_join(table1, table2)

EDIT: As above commenter said, a full_join is a better habit to get into using, though a right_join will get the job done here.

Anoraka
  • 11
  • 5
1

We can use left_join on table2

library(dplyr)
left_join(table2, table1)

by is not really needed here as it will pick it from the matching names

akrun
  • 874,273
  • 37
  • 540
  • 662