d1 <- tibble::tribble(
~id1, ~id2, ~results1,
"a", "b", 10L,
"a", "c", 20L,
"c", "b", 30L
)
d2 <- tibble::tribble(
~id1, ~id2, ~results2,
"b", "a", 101L,
"c", "a", 201L,
"b", "c", 301L
)
# Inner join fails because id1!=id2.
dplyr::inner_join(d1, d2, by=c("id1", "id2"))
# Arrange IDs
d1 %>% arrange_ids(id1, id2)
d2 %>% arrange_ids(id1, id2)
# Inner join
dplyr::inner_join(arrange_ids(d1, id1, id2), arrange_ids(d2, id1, id2), by=c("id1", "id2"))
# Recursively, if you had more than two tibbles
list(d1, d2) %>%
purrr::map(arrange_ids, id1, id2) %>%
purrr::reduce(dplyr::inner_join, by=c("id1", "id2"))
Run the code above in your browser using DataLab