# from igraph to adjacency matrix
# sparse matrix
as_adjacency_matrix(g)
## 5 x 5 sparse Matrix of class "dgCMatrix"
## Alice Bob Cecil David Esmeralda
## Alice . . . . .
## Bob 1 . . . .
## Cecil 1 1 . . .
## David 1 1 . . .
## Esmeralda 1 . . . .
# non-sparse matrix
as_adjacency_matrix(g, sparse = FALSE)
## Alice Bob Cecil David Esmeralda
## Alice 0 0 0 0 0
## Bob 1 0 0 0 0
## Cecil 1 1 0 0 0
## David 1 1 0 0 0
## Esmeralda 1 0 0 0 0
# weighted matrix
as_adjacency_matrix(g, attr = "friendship")
## 5 x 5 sparse Matrix of class "dgCMatrix"
## Alice Bob Cecil David Esmeralda
## Alice . . . . .
## Bob 4 . . . .
## Cecil 5 5 . . .
## David 2 1 . . .
## Esmeralda 1 . . . .
# from adjacency matrix to igraph
# non-weighted graph
(A = as_adjacency_matrix(g))
## 5 x 5 sparse Matrix of class "dgCMatrix"
## Alice Bob Cecil David Esmeralda
## Alice . . . . .
## Bob 1 . . . .
## Cecil 1 1 . . .
## David 1 1 . . .
## Esmeralda 1 . . . .
graph_from_adjacency_matrix(A)
## IGRAPH 1b1c4f0 DN-- 5 6 --
## + attr: name (v/c)
## + edges from 1b1c4f0 (vertex names):
## [1] Bob ->Alice Cecil ->Alice David ->Alice Esmeralda->Alice
## [5] Cecil ->Bob David ->Bob
# undirected graph
graph_from_adjacency_matrix(A, mode = "undirected")
## IGRAPH 1c2f755 UN-- 5 6 --
## + attr: name (v/c)
## + edges from 1c2f755 (vertex names):
## [1] Alice--Bob Alice--Cecil Alice--David Alice--Esmeralda
## [5] Bob --Cecil Bob --David
(A = as_adjacency_matrix(g, attr = "friendship"))
## 5 x 5 sparse Matrix of class "dgCMatrix"
## Alice Bob Cecil David Esmeralda
## Alice . . . . .
## Bob 4 . . . .
## Cecil 5 5 . . .
## David 2 1 . . .
## Esmeralda 1 . . . .
# multi-graph
graph_from_adjacency_matrix(A)
## IGRAPH 6540ab8 DN-- 5 18 --
## + attr: name (v/c)
## + edges from 6540ab8 (vertex names):
## [1] Bob ->Alice Bob ->Alice Bob ->Alice Bob ->Alice
## [5] Cecil ->Alice Cecil ->Alice Cecil ->Alice Cecil ->Alice
## [9] Cecil ->Alice David ->Alice David ->Alice Esmeralda->Alice
## [13] Cecil ->Bob Cecil ->Bob Cecil ->Bob Cecil ->Bob
## [17] Cecil ->Bob David ->Bob
# weighted graph on weight attribute
graph_from_adjacency_matrix(A, weighted = TRUE)
## IGRAPH d8d0a35 DNW- 5 6 --
## + attr: name (v/c), weight (e/n)
## + edges from d8d0a35 (vertex names):
## [1] Bob ->Alice Cecil ->Alice David ->Alice Esmeralda->Alice
## [5] Cecil ->Bob David ->Bob
# weighted graph on friendship attribute
graph_from_adjacency_matrix(A, weighted = "friendship")
## IGRAPH 06b864c DN-- 5 6 --
## + attr: name (v/c), friendship (e/n)
## + edges from 06b864c (vertex names):
## [1] Bob ->Alice Cecil ->Alice David ->Alice Esmeralda->Alice
## [5] Cecil ->Bob David ->Bob