1

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?

ilex
  • 103
  • 7
  • You can search for igraph cluster in stackoverflow – chinsoon12 Jan 24 '20 at 10:32
  • It might be that the heading of this post is not really clear or suitable to the question. In case somebody would like to edit it, it might be easier to find. – ilex Jan 24 '20 at 11:19
  • see https://stackoverflow.com/questions/45079559/make-a-group-indices-based-on-several-columns – chinsoon12 Jan 28 '20 at 00:41

1 Answers1

0
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.frame(x1, x2)

dt$x3=dt$x1
dt
for(i in 1:nrow(dt)){
  if(dt$x3[i]!=dt$x2[i]){
    dt$x3[dt$x3==dt$x2[i]]=dt$x3[i]
  }
}
setDT(dt)[, id := .GRP, by=x3]
dt
  1. Create duplicate of x1, x3
  2. Iterate through x3, check if different from x2
  3. If different, replaces all elements in x3 which are equal to the element that you just checked in x2 with the current value of x3
  4. Assign ID's with setDT function
gallardoelise
  • 342
  • 3
  • 17