【问题标题】:Create an igraph object from an adjacency list with edge attributes in R在 R 中从具有边缘属性的邻接列表创建 igraph 对象
【发布时间】:2014-05-28 19:45:49
【问题描述】:

我有一个 4 列的邻接矩阵。前 2 列是我想成为 igraph 对象中的顶点的源节点和目标节点。我可以使用下面的代码来实现这一点。

al <- data.frame(sourceNode=c('a', 'a', 'b', 'c'), 
             consumerNode=c('b', 'c', 'c', 'a'), 
             edgeAtt1=c('highway', 'road', 'road', 'path'),
             edgeAtt2=c('1999', '2010', '2014', '1999'))

require('igraph')
g <- graph.edgelist(as.matrix(al[,c('sourceNode', 'consumerNode')]))

但是,当我创建这个 igraph 对象时,我想做的是包含来自 al 的第 3 列和第 4 列作为边缘属性。

一些函数 functionThatINeed 让我可以做这样的事情:

g &lt;- functionThatINeed(al[,c('sourceNode', 'consumerNode')]), edgeAttributes=al[,c('edgeAtt1', 'edgeAtt2')])

【问题讨论】:

    标签: r igraph


    【解决方案1】:

    创建图表时不能这样做,但可以在使用edge.attributes() 之后立即进行

    require('igraph')
    g <- graph.edgelist(as.matrix(al[,c('sourceNode', 'consumerNode')]))
    edge.attributes(g) <- al[,c('edgeAtt1', 'edgeAtt2')]
    

    如果你真的想,你可以创建你自己的函数

    graph.edgelist.attributes <- function(et, at=NULL, directed=F) {
        g <- graph.edgelist(el, directed)
        edge.attributes(g) <- at
        g
    }
    

    【讨论】:

      【解决方案2】:

      我想我真的找到了答案。 igraph 包中的graph.data.frame 允许您从一个提供边缘属性的 edgelist 数据帧和提供节点属性的第二个数据帧创建一个 igraph 对象。

      http://www.inside-r.org/packages/cran/igraph/docs/graph.data.frame

      【讨论】:

        猜你喜欢
        • 2021-06-27
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-04-14
        • 2018-04-25
        • 1970-01-01
        • 2017-08-06
        • 2014-09-04
        相关资源
        最近更新 更多