I am trying to associate pairs of unique identifiers using R. Given the example below, I have two identifiers (here ID1 and ID2) that indicate a relationship. I am trying to create groups of strings that are related. In this example, A is linked to B, which is linked to D, which is linked to E. Since they are all connected, I want to group them together. Further, there is also X, which is associated with both Y and Z. Since these two are also related, I want to assign them to the same group. How can I solve this problem with R?
Thank!
Sample data:
ID1 ID2
A B
B D
D E
X Y
X Z
DPUT R view
structure(list(id1 = structure(c(1L, 2L, 3L, 4L, 4L), .Label = c("A", "B", "D", "X"), class = "factor"), id2 = structure(1:5,.Label = c("B", "D", "E", "Y", "Z"), class = "factor")), .Names = c("id1", "id2"), row.names = c(NA, -5L), class = "data.frame")
Required Conclusion:
ID1 ID2 GROUP
A B 1
B D 1
D E 1
X Y 2
X Z 2
source
share