Reputation: 389
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
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
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
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