0
votes

I have an adjacency matrix with 4 columns. The first 2 columns are the source and target nodes that I want to become vertices in my igraph object. 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.

Some function functionThatINeed that lets me do something like this:

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

2

2 Answers

3
votes

You can't do it when you create the graph, but you can do it immediately after with 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
}
0
votes

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.

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