0

Note 1: I'm using the R packages "network" and "sna"

Note 2: My original data are in edgelist format in a .csv file.

I've been looking for the best way to read in edgelist data into R. At first sight, this is simple.

data <- read.csv("file.CSV", header=F)
network <- network(data, matrix.type="edgelist", directed=F) #convert data to network object
val<-data[,3] #here are the values for my edges
set.edge.value(network, "val", val, e=1:length(network$mel))

When I ask the network to return edge values (get.edge.values), it returns the correct values.

However, when I ask summary(network), it just returns an adjacency matrix where all values are set to 1 (with exception of the diagonal). Even if they had the value of zero, they get a value of 1.

Moreover, trying to get something like degree(network) returns wrong results.

I have been searching for days on this. A possible solution was to use network2<-as.matrix.network(netwerk1, matrix.type="adjacency", attrname="val"). This works. The problem, however, is that this ceases to be a network object and becomes a matrix class. As a result, I cannot add vertex attributes to the network. Converting network2 back to a network object again looses the edge values in the network.

Some help would really be appreciated.

Best, Frederik

1 Answers1

0

Looks like you answered your own question: when converting a network object to a matrix, you need to include the attrname argument to tell as.matrix.network which attribute to use for the weights. Can you explain why you need to convert it back to a network object if it already is one?

The degree function in the sna does not have an argument to tell it which edge attribute to use, so you have to pass it a matrix that has the weights.

> library(network)
> library(sna)
> net<-network.initialize(3)
> add.edges(net,c(1,2,3),c(2,3,1))
> set.edge.attribute(net,'weight',c(5,6,7))
> as.matrix(net,matrix.type='adjacency',attrname='weight')
  1 2 3
1 0 5 0
2 0 0 6
3 7 0 0
> netmat<-as.matrix(net,matrix.type='adjacency',attrname='weight')
> degree(net)
[1] 2 2 2
> degree(netmat)
[1] 12 11 13

So in your case,

degree(network2) 

should work.

skyebend
  • 1,079
  • 6
  • 19
  • Well, the reason for wanting a network object is so I can run network related functions on it. Using as.matrix.network does not yield a network object. However, I did find a solution using the igraph package. – Frederik Pederik Apr 03 '13 at 13:41