繁体   English   中英

使用R中的边属性从邻接表创建igraph对象

[英]Create an igraph object from an adjacency list with edge attributes in R

我有一个4列的邻接矩阵。 前两列是我要成为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 <- functionThatINeed(al[,c('sourceNode', 'consumerNode')]), edgeAttributes=al[,c('edgeAtt1', 'edgeAtt2')])

创建图形时无法执行此操作,但是可以在使用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
}

我想我确实找到了答案。 igraph包中的graph.data.frame允许您从一个提供边缘属性的边缘列表数据框和另一个提供节点属性的data.frame创建一个igraph对象。

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM