Imagine we have a table like this one:
> my_table <- matrix(c(1,1,2,3,4,5,7,8,9,10,2,3,5,5,5,6,9,10,11,11), ncol=2)
> names <- c("FROM", "TO")
> colnames(my_table) <- names
> my_table
FROM TO
[1,] 1 2
[2,] 1 3
[3,] 2 5
[4,] 3 5
[5,] 4 5
[6,] 5 6
[7,] 7 9
[8,] 8 10
[9,] 9 11
[10,] 10 11
This table represents relationships between items, like this:
My question is, how can the "cases" be distinguished using R?
The result should be:
FROM TO CASE
[1,] 1 2 1
[2,] 1 3 1
[3,] 2 5 1
[4,] 3 5 1
[5,] 4 5 1
[6,] 5 6 1
[7,] 7 9 2
[8,] 8 10 2
[9,] 9 11 2
[10,] 10 11 2
Note that each item can only be present in one "case", i.e., item 1 and 7 could not be linked in the example above, if so there would be only a single case:
Not possible in the example above
If so, it would have to be like this