假设我们有一个这样的表:
> 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
此表表示项目之间的关系,如下所示:
我的问题是,如何使用R区分“案件”??
结果应为:
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
请注意,每个项目只能出现在一个“案例”中,即,在上面的示例中,项目1和7不能链接,如果这样的话,只会存在一个案例:
Not possible in the example above
If so, it would have to be like this