Grouping Unique Identification Pairs Using R

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
+4
source share
1 answer

@Frank, igraph:

library(igraph)
idf <- graph.data.frame(df)
clusters(idf)$membership

:

A B D X E Y Z 
1 1 1 2 1 2 2 

df:

merge(df, stack(clusters(idf)$membership), by.x = "id1", by.y = "ind", all.x = TRUE)
+10

Source: https://habr.com/ru/post/1649560/


All Articles