使用R分组链接的唯一ID对 [英] Grouping linked unique ID pairs using R
问题描述
我正在尝试使用R链接对唯一的ID。给定下面的示例,我有两个ID(这里是ID1和ID2),指示链接。我正在尝试创建链接的行组。在这个例子中,A链接到与D链接的D,因为这些都是连接的,所以我想把它们组合在一起。接下来,还有连接到Y和Z的X。因为这两个也被连接,所以我想将它们分配给一个单独的组。如何处理这个使用R?
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?
谢谢!
示例数据
ID1 ID2
A B
B D
D E
X Y
X Z
DPUT R表示
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")
需要输出
ID1 ID2 GROUP
A B 1
B D 1
D E 1
X Y 2
X Z 2
推荐答案
根据@Frank在评论中的提及,您可以使用 igraph
:
As per mentionned by @Frank in the comments, you can use 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
:
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)
这篇关于使用R分组链接的唯一ID对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!