Given is a data.table representing the relations between 6 objects:
# create sampla data.table
x1 <- c(1,1,1,2,2,2,3,3,3,4,5,6)
x2 <- c(1,2,3,1,2,3,1,2,3,4,6,5)
dt <- data.table(x1, x2)
1st row represents the objects. 2nd row represents connection with other objects.
# check combinations
dt[dt$x1 != dt$x2]
Object 4 has no connections with other objects. Objects 1, 2 and 3 are connected, as well as objects 5 and 6.
Now, a new column should be created where all connected objects get the same number (ID)
The resulting data.table should look like:
x3 <- c(1,1,1,1,1,1,1,1,1,2,3,3)
dt.res <- data.table(dt, x3)
How can this be achieved?