In my dataframe, I have a 'Groups' column and a 'Person' column. I want to join groups together if they share at least one common person. Consider the following example data:
Group Person
1 David
1 Sarah
1 John
2 Brian
2 Andrew
3 David
3 Charlie
4 Clare
4 Greg
5 Greg
5 Clare
5 Alan
In this example, Group 1 and Group 3 share a common person - David. The people in Group 2 do not overlap with the people in any other group. Group 4 and Group 5 share two common people Clare and Greg.
My desired output would be as follows:
Group Person
1 David
1 Sarah
1 John
1 Charlie
2 Brian
2 Andrew
3 Clare
3 Greg
3 Alan
Reproducible data:
structure(list(Group = c(1, 1, 1, 2, 2, 3, 3, 4, 5, 5), Person = c("David",
"Sarah", "John", "Brian", "Andrew", "David", "Charlie", "Clare",
"Greg", "Clare")), class = c("spec_tbl_df", "tbl_df", "tbl",
"data.frame"), row.names = c(NA, -10L), spec = structure(list(
cols = list(Group = structure(list(), class = c("collector_double",
"collector")), Person = structure(list(), class = c("collector_character",
"collector"))), default = structure(list(), class = c("collector_guess",
"collector")), skip = 1), class = "col_spec"))