[英]Create an igraph object from an adjacency list with edge attributes in R
I have an adjacency matrix with 4 columns. 我有一个4列的邻接矩阵。 The first 2 columns are the source and target nodes that I want to become vertices in my igraph object.
前两列是我要成为igraph对象中的顶点的源节点和目标节点。 I can achieve this with the code below.
我可以使用下面的代码来实现。
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')]))
However, what I want to do is include columns 3 and 4 from al
as edge attributes when I create this igraph object. 但是,当我创建此igraph对象时,我想做的是将
al
3列和第4列作为边缘属性。
Some function functionThatINeed
that lets me do something like this: 一些函数
functionThatINeed
让我可以执行以下操作:
g <- functionThatINeed(al[,c('sourceNode', 'consumerNode')]), edgeAttributes=al[,c('edgeAtt1', 'edgeAtt2')])
You can't do it when you create the graph, but you can do it immediately after with edge.attributes()
创建图形时无法执行此操作,但是可以在使用
edge.attributes()
之后立即执行。
require('igraph')
g <- graph.edgelist(as.matrix(al[,c('sourceNode', 'consumerNode')]))
edge.attributes(g) <- al[,c('edgeAtt1', 'edgeAtt2')]
If you really wanted to, you could create you own function 如果您真的想要,可以创建自己的函数
graph.edgelist.attributes <- function(et, at=NULL, directed=F) {
g <- graph.edgelist(el, directed)
edge.attributes(g) <- at
g
}
I think I actually found the answer. 我想我确实找到了答案。
graph.data.frame
from the igraph package allows you to create an igraph object from one edgelist dataframe which provides edge attributes and a second data.frame which provides node attributes. igraph包中的
graph.data.frame
允许您从一个提供边缘属性的边缘列表数据框和另一个提供节点属性的data.frame创建一个igraph对象。
http://www.inside-r.org/packages/cran/igraph/docs/graph.data.frame http://www.inside-r.org/packages/cran/igraph/docs/graph.data.frame
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.