R igraph - Convert a weighted adjacency matrix into weighted edgelist

吃可爱长大的小学妹 提交于 2019-12-07 08:01:07

问题


I have a nxm adjacency matrix, where (i,j) represent the score of association between i and j. I need to convert this into the following format like : i j <score1>

using R' igraph package and output it into a text file.

I can derive the edgelist, but its showing up without the weights. I used the following code:

library(igraph) g <- graph.adjacency(myAdjacencymatrix) get.edgelist(g)

However, it does not show the weights.


回答1:


library(igraph)
set.seed(1)                # for reproducible example
myAdjacencyMatrix <- matrix(runif(400),nc=20,nr=20)

g  <- graph.adjacency(myAdjacencyMatrix,weighted=TRUE)
df <- get.data.frame(g)
head(df)
#   from to    weight
# 1    1  1 0.2655087
# 2    1  2 0.9347052
# 3    1  3 0.8209463
# 4    1  4 0.9128759
# 5    1  5 0.4346595
# 6    1  6 0.6547239

You need to use weighted=TRUE in the call to graph.adjacency(...) to have weights assigned to the edges. Then, get.data.frame(...) will return a data frame of the edges with all edge attributes by default. You can use the what=... argument to return, e.g., the vertex list with attributes.

In future: provide an example, rather than forcing us to create one for you!!!



来源:https://stackoverflow.com/questions/24742882/r-igraph-convert-a-weighted-adjacency-matrix-into-weighted-edgelist

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!