R igraph-将加权邻接矩阵转换为加权边列表 [英] R igraph - Convert a weighted adjacency matrix into weighted edgelist
问题描述
我有一个nxm邻接矩阵,其中(i,j)表示i和j之间的关联分数.我需要将其转换为以下格式,例如:
i j <score1>
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>
使用R'igraph软件包并将其输出到文本文件中.
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)
library(igraph)
g <- graph.adjacency(myAdjacencymatrix)
get.edgelist(g)
但是,它不显示权重.
推荐答案
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
您需要在对graph.adjacency(...)
的调用中使用weighted=TRUE
,以将权重分配给边缘.然后,get.data.frame(...)
默认情况下将返回具有所有边缘属性的边缘数据帧.您可以使用what=...
参数返回例如具有属性的顶点列表.
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.
将来:提供一个示例,而不是强迫我们为您创建一个示例!
这篇关于R igraph-将加权邻接矩阵转换为加权边列表的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!