Category Archives: R

Extracting adjacency matrices with valued edges

This may seem obvious to an expert statnet user, but it took me a bit of careful reading of Carter's paper and some trial and error to figure it out. We are using the frequency of behaviors based on ethological observations as edge weights and would like to be able to extract a matrix of the edge weights.

[r]
set.seed(123)
## generate a network with 21 nodes and 50 edges.
## some edges are either self-loops or redundant
## just a quick and dirty way to get an example network object

n1 <- round(1+20*runif(50)) n2 <- round(1+20*runif(50)) n3 <- rpois(50,3) eee <- cbind(n1,n2)[order(n1),] net <- network(eee,directed=FALSE) set.edge.attribute(net,"meaningful.measure",n3) as.matrix(net,attrname="meaningful.measure") [/r] This last command returns a 50x50 matrix of the edge weights.

Extracting an edge list from a network object

I've been using the statnet suite of tools a lot recently.  As with any powerful software, there is quite a learning curve.  I will write some notes in my blog to help me remember tricks that I learn along the way.  And who knows? They might even be useful to other people!

For a variety of reasons, we have found it easy to import our networks as adjacency matrices.  The problem is that when there are attributes associated with the edges, it is much easier to deal with an edge list.  While using summary(net) yields an edge list as part of the summary, it was not clear to me how to get such a list as a manipulable object.  I wrote the statnet_help list and Carter Butts (co-author of network) pointed out to me that getting an edgelist is quite simple. Having read in our adjacency matrix
[r] mat <-read.table("mat5.txt",header=TRUE) mat <- as.matrix(mat) net <- network(mat, directed=FALSE)[/r] We can get the edge list like this: [r]bbb <- as.matrix(net,matrix.type="edgelist") [/r] Easy...