Grouping linked unique ID pairs using R [duplicate]

烂漫一生 提交于 2019-12-04 03:43:59

问题


I'm trying to link together pairs of unique IDs using R. Given the example below, I have two IDs (here ID1 and ID2) that indicate linkage. I'm trying to create groups of rows that are linked. In this example A is linked to B which is linked to D which is linked to E. Because these are all connected, I want to group them together. Next, there is also X which is linked to both Y and Z. Because these two are also connected, I want to assign them to a single group as well. How can I tackle this using R?

Thanks!

Example data:

ID1 ID2
A   B
B   D
D   E
X   Y
X   Z

DPUT R representation

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")

Output needed:

ID1 ID2 GROUP
A   B   1
B   D   1
D   E   1
X   Y   2
X   Z   2

回答1:


As per mentionned by @Frank in the comments, you can use igraph:

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

Which gives:

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

Should you want to assign the result back to rows of df:

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


来源:https://stackoverflow.com/questions/38663170/grouping-linked-unique-id-pairs-using-r

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!