Rez99
Rez99

Reputation: 389

How can I join 2 tables with dplyr and keep all columns from the RHS table?

Here are 2 data frames:

df1 <- data.frame(ID=c(1,2,3,4))
df1
df2 <- data.frame(ID=c(1,3))
df2

How can I join them to get the following output?:

#  ID.1 ID.2
#     1    1
#     2 
#     3    3
#     4 

Thanks!

Upvotes: 3

Views: 9604

Answers (3)

Dmitry Zotikov
Dmitry Zotikov

Reputation: 2532

Try dplyr::left_join with keep = TRUE:

> left_join(df1, df2, keep = TRUE, suffix = c('.1', '.2'), by = 'ID')
  ID.1 ID.2
1    1    1
2    2   NA
3    3    3
4    4   NA

Upvotes: 6

Jordo82
Jordo82

Reputation: 816

You could duplicate the ID column in df2 prior to the join:

library(tidyverse)

df1 <- data.frame(ID=c(1,2,3,4))
df2 <- data.frame(ID=c(1,3)) %>% 
  mutate(ID.2 = ID)

df1 %>% 
  left_join(df2, by = c("ID" = "ID"))

  ID ID.2
1  1    1
2  2   NA
3  3    3
4  4   NA

Upvotes: 2

akrun
akrun

Reputation: 887881

An option would be to use match

data.frame(ID.1 = df1$ID, ID.2 = df2$ID[match(df1$ID, df2$ID)])
#   ID.1 ID.2
#1    1    1
#2    2   NA
#3    3    3
#4    4   NA

Upvotes: 3

Related Questions