Reputation: 463
I would like to merge dataframes in R such that only only those observations whose rows partially correspond across the dataframes are kept.
I have two dataframes (These are toy dataframes - the actual ones have hundreds of columns.):
V1 V2 V3
rabbit 001 M
squirrel 001 M
cow 001 M
rabbit 004 M
squirrel 004 M
skunk 004 M
V1 V2 V3
rabbit 001 B
squirrel 001 B
skunk 001 B
rabbit 004 B
squirrel 004 B
skunk 008 B
Desired outcome:
V1 V2 V3
rabbit 001 M
squirrel 001 M
rabbit 004 M
squirrel 004 M
rabbit 001 B
squirrel 001 B
rabbit 004 B
squirrel 004 B
merge and dplyr::inter_join aren't quite the right functions for this. What is?
Upvotes: 1
Views: 225
Reputation: 12155
d.b's answer is likely much more efficient, but if you prefer to think about the problem in terms of JOIN operations, you can do this with 3 dplyr
join operations:
library(dplyr)
# Perform an inner_join with just the columns that you want to match
match_rows <- inner_join(df1[,1:2], df2[,1:2])
match_rows
V1 V2
1 rabbit 1
2 squirrel 1
3 rabbit 4
4 squirrel 4
# Then left_join that with each dataframe to get the matching rows from each
# and then bind them together as rows
bind_rows(left_join(match_rows, df1),
left_join(match_rows, df2))
V1 V2 V3
1 rabbit 1 M
2 squirrel 1 M
3 rabbit 4 M
4 squirrel 4 M
5 rabbit 1 B
6 squirrel 1 B
7 rabbit 4 B
8 squirrel 4 B
Upvotes: 1
Reputation: 32538
rbind(d1, d2)[ave(1:(nrow(d1) + nrow(d2)),
Reduce(paste, rbind(d1, d2)[c("V1", "V2")]),
FUN = length) > 1,]
# V1 V2 V3
#1 rabbit 1 M
#2 squirrel 1 M
#4 rabbit 4 M
#5 squirrel 4 M
#7 rabbit 1 B
#8 squirrel 1 B
#10 rabbit 4 B
#11 squirrel 4 B
#dput(d1)
structure(list(V1 = c("rabbit", "squirrel", "cow", "rabbit",
"squirrel", "skunk"), V2 = c(1L, 1L, 1L, 4L, 4L, 4L), V3 = c("M",
"M", "M", "M", "M", "M")), row.names = c(NA, 6L), class = "data.frame")
#dput(d2)
structure(list(V1 = c("rabbit", "squirrel", "skunk", "rabbit",
"squirrel", "skunk"), V2 = c(1L, 1L, 1L, 4L, 4L, 8L), V3 = c("B",
"B", "B", "B", "B", "B")), row.names = 7:12, class = "data.frame")
Upvotes: 2