将邻接矩阵转换为From To矩阵

时间:2016-01-11 22:29:22

标签: r graph transformation reshape adjacency-matrix

我正在尝试了解图表,我正在使用玩具数据集,如下所示

df = structure(list(A = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("0","1"), class = "factor"), 
                    B= structure(c(1L, 1L,2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L, 1L, 1L, 2L, 2L), .Label = c("0","1"), class = "factor"), 
                    C= structure(c(1L,1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L), .Label = c("0","1"), class = "factor"), 
                    D= structure(c(1L,1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("0","1"), class = "factor"), 
                    Weight = c(12, 33, 65, 72, 9, 3, 3, 10, 17, 9, 9, 25, 1, 0, 1, 4)), .Names = c("A","B", "C", "D", "Weight"), 
                    row.names = c(NA, -16L), class = "data.frame")

我有兴趣将上面的这个邻接矩阵转换为From和To矩阵,就像这样。

         From     To       Weight
         A        B        72
         A        C        3
         C        D        1
         .        .        .
         .        .        .
         A        D        9

所以,任何有关如何实现这一目标的帮助都非常感谢。

------------- user20650更正-----------

df = df[-1,]排除第一次观察

0 个答案:

没有答案