We could use tidyr::expand() to generate all combinations of col1 and col2 and then join p1 and p2 to retrieve the remaining columns.
library(dplyr, warn.conflicts = FALSE)
p1 <- tribble(
~participant, ~bodypart, ~col,
"left", "head", 1,
"left", "body", 2
)
p2 <- tribble(
~participant, ~bodypart, ~col,
"right", "head", 3,
"right", "body", 4
)
p1 %>%
bind_cols(p2) %>%
tidyr::expand(col, col1) %>%
left_join(p1, by = "col") %>%
left_join(p2, by = c("col1" = "col")) %>%
select(participant1 = participant.x, bodypart1 = bodypart.x, col1 = col,
participant2 = participant.y, bodypart2 = bodypart.y, col2 = col1)
#> # A tibble: 4 x 6
#> participant1 bodypart1 col1 participant2 bodypart2 col2
#> <chr> <chr> <dbl> <chr> <chr> <dbl>
#> 1 left head 1 right head 3
#> 2 left head 1 right body 4
#> 3 left body 2 right head 3
#> 4 left body 2 right body 4
Created on 2020-05-19 by the reprex package (v0.3.0)