Grouping linked unique ID pairs using R [duplicate]

为君一笑 提交于 2019-12-01 20:03:33

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