If I have a data frame with two ID columns A
and B
, where each observation represents an edge (connection between two ids), what is the best way to determine all the disjoint ID groups? IDs can be present in either column and repeated.
By way of example, here is a test data frame along with what I would expect as a result:
df <- data.frame(A = rep(1:5, 2), B = c(3, 7:15))
# A B
# 1 3
# 2 7
# 3 8
# 4 9
# 5 10
# 1 11
# 2 12
# 3 13
# 4 14
# 5 15
# Proposed results
# Each element of the list represents a unique group
# [[1]]
# [1] 1 3 8 11 13
#
# [[2]]
# [1] 2 7 12
#
# [[3]]
# [1] 4 9 14
#
# [[4]]
# [1] 5 10 15