[英]R iGraph: How to get weighted adjacency matrix from a graph?
While there are some questions dealing with creating a graph from an adjacency matrix, I haven't found much about extracting the weighted adjacency matrix from a weighted graph. 虽然存在一些有关从邻接矩阵创建图的问题,但是我对从加权图提取加权邻接矩阵的了解还很少。
Say I have the following graph: 说我有以下图形:
library(igraph)
nodes <- data.frame(name=c("a","b", "c", "d", "f", "g"))
col1 <- c("a", "g", "f","f", "d","c")
col2 <- c("b", "f","c","d","a","a")
weight <- c(1,4,2,6,2,3)
edges <- cbind.data.frame(col1,col2,weight)
g <- graph.data.frame(edges, directed=F, vertices=nodes)
E(g)$weight <- weight
How do I get the weighted adjacency matrix of graph g? 如何获得图g的加权邻接矩阵?
It appears there are actually quite a few ways to do this. 看来实际上有很多方法可以做到这一点。 Perhaps obvious, a first way to do it is to look carefully at the documentation of
as_adjacency_matrix()
and using the attr
option: 也许很明显,第一种方法是仔细查看
as_adjacency_matrix()
的文档并使用attr
选项:
as_adjacency_matrix(g,attr = "weight",sparse = T)
6 x 6 sparse Matrix of class "dgCMatrix"
a b c d f g
a . 1 3 2 . .
b 1 . . . . .
c 3 . . . 2 .
d 2 . . . 6 .
f . . 2 6 . 4
g . . . . 4 .
But one can also type 但是也可以输入
get.adjacency(g,attr = "weight",sparse = T)
Or just 要不就
g[]
6 x 6 sparse Matrix of class "dgCMatrix"
a b c d f g
a . 1 3 2 . .
b 1 . . . . .
c 3 . . . 2 .
d 2 . . . 6 .
f . . 2 6 . 4
g . . . . 4 .
Even if I'm not sure of the scope of validity of this last option. 即使我不确定最后一个选项的有效性范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.