1
0
Entering edit mode
5.2 years ago

Hi!

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

R • 1.1k views
0
Entering edit mode

A piece of etiquette about posting questions on online forums: avoid cross posting to multiple websites.

0
Entering edit mode
5.2 years ago