1

I have matrix that through the interpretation as a network shall give me an edgelist with the matrix' values as weights.

M4 <- matrix(rpois(36,5),nrow=6) #randomly googled that to have sample data
Mg <- graph.adjacency(M4) # make matrix into graph
Mw <- get.data.frame(Mg) # 1st try
Mw2 <- cbind( get.edgelist(Mg) , round( E(Mg)$weight, 3 )) # second try

You'll probably see that Mw using get.data.frame is giving me an edgelist, but without the edge weight. There is probably an attribute, which I can't find or understand.

Mw2 using this solution returns:

Error in round(E(Mg)$weight, 3) : non-numeric argument to mathematical function.

How to work around that? Leaves me puzzled.

slinel
  • 61
  • 7

1 Answers1

2

If you would like to assign weights to edges, you should enable the option weighted like below

Mg <- graph.adjacency(M4, weighted = TRUE)

and finally you will get Mw2 like

> Mw2
      [,1] [,2] [,3]
 [1,]    1    1    2
 [2,]    1    2    9
 [3,]    1    3    4
 [4,]    1    4    4
 [5,]    1    5    7
 [6,]    1    6    2
 [7,]    2    1    4
 [8,]    2    2    1
 [9,]    2    3    4
[10,]    2    4    5
[11,]    2    5    5
[12,]    2    6    4
[13,]    3    1    5
[14,]    3    2    2
[15,]    3    3    6
[16,]    3    4    5
[17,]    3    5    6
[18,]    3    6    4
[19,]    4    1    3
[20,]    4    2    4
[21,]    4    3    9
[22,]    4    4    8
[23,]    4    5    3
[24,]    4    6    5
[25,]    5    1    7
[26,]    5    2    5
[27,]    5    3    3
[28,]    5    4    3
[29,]    5    5    5
[30,]    5    6    6
[31,]    6    1    2
[32,]    6    2    7
[33,]    6    3    4
[34,]    6    4    7
[35,]    6    5   11
[36,]    6    6    3
ThomasIsCoding
  • 96,636
  • 9
  • 24
  • 81