Sometimes it is useful to work with a standard representation of a graph, like an adjacency matrix.
Usage
as_adjacency_matrix(
graph,
type = c("both", "upper", "lower"),
attr = NULL,
edges = deprecated(),
names = TRUE,
sparse = igraph_opt("sparsematrices")
)
Arguments
- graph
The graph to convert.
- type
Gives how to create the adjacency matrix for undirected graphs. It is ignored for directed graphs. Possible values:
upper
: the upper right triangle of the matrix is used,lower
: the lower left triangle of the matrix is used.both
: the whole matrix is used, a symmetric matrix is returned.- attr
Either
NULL
or a character string giving an edge attribute name. IfNULL
a traditional adjacency matrix is returned. If notNULL
then the values of the given edge attribute are included in the adjacency matrix. If the graph has multiple edges, the edge attribute of an arbitrarily chosen edge (for the multiple edges) is included. This argument is ignored ifedges
isTRUE
.Note that this works only for certain attribute types. If the
sparse
argumen isTRUE
, then the attribute must be either logical or numeric. If thesparse
argument isFALSE
, then character is also allowed. The reason for the difference is that theMatrix
package does not support character sparse matrices yet.- edges
Logical scalar, whether to return the edge ids in the matrix. For non-existant edges zero is returned.
- names
Logical constant, whether to assign row and column names to the matrix. These are only assigned if the
name
vertex attribute is present in the graph.- sparse
Logical scalar, whether to create a sparse matrix. The ‘
Matrix
’ package must be installed for creating sparse matrices.
Details
as_adjacency_matrix()
returns the adjacency matrix of a graph, a
regular matrix if sparse
is FALSE
, or a sparse matrix, as
defined in the ‘Matrix
’ package, if sparse
if
TRUE
.
See also
graph_from_adjacency_matrix()
, read_graph()
Other conversion:
as.matrix.igraph()
,
as_adj_list()
,
as_biadjacency_matrix()
,
as_data_frame()
,
as_directed()
,
as_edgelist()
,
as_graphnel()
,
as_long_data_frame()
,
graph_from_adj_list()
,
graph_from_graphnel()
Examples
g <- sample_gnp(10, 2 / 10)
as_adjacency_matrix(g)
#> 10 x 10 sparse Matrix of class "dgCMatrix"
#>
#> [1,] . . . . . . . 1 1 .
#> [2,] . . 1 . . . . . . .
#> [3,] . 1 . . . . 1 1 . .
#> [4,] . . . . . . . . . .
#> [5,] . . . . . 1 . 1 . .
#> [6,] . . . . 1 . 1 . . .
#> [7,] . . 1 . . 1 . . . 1
#> [8,] 1 . 1 . 1 . . . . 1
#> [9,] 1 . . . . . . . . 1
#> [10,] . . . . . . 1 1 1 .
V(g)$name <- letters[1:vcount(g)]
as_adjacency_matrix(g)
#> 10 x 10 sparse Matrix of class "dgCMatrix"
#> [[ suppressing 10 column names ‘a’, ‘b’, ‘c’ ... ]]
#>
#> a . . . . . . . 1 1 .
#> b . . 1 . . . . . . .
#> c . 1 . . . . 1 1 . .
#> d . . . . . . . . . .
#> e . . . . . 1 . 1 . .
#> f . . . . 1 . 1 . . .
#> g . . 1 . . 1 . . . 1
#> h 1 . 1 . 1 . . . . 1
#> i 1 . . . . . . . . 1
#> j . . . . . . 1 1 1 .
E(g)$weight <- runif(ecount(g))
as_adjacency_matrix(g, attr = "weight")
#> 10 x 10 sparse Matrix of class "dgCMatrix"
#> [[ suppressing 10 column names ‘a’, ‘b’, ‘c’ ... ]]
#>
#> a . . . . . . . 0.125495772
#> b . . 0.9695255 . . . . .
#> c . 0.9695255 . . . . 0.14713281 0.514231354
#> d . . . . . . . .
#> e . . . . . 0.00332753 . 0.003141262
#> f . . . . 0.003327530 . 0.60409122 .
#> g . . 0.1471328 . . 0.60409122 . .
#> h 0.1254958 . 0.5142314 . 0.003141262 . . .
#> i 0.1863833 . . . . . . .
#> j . . . . . . 0.02471268 0.330121691
#>
#> a 0.186383283 .
#> b . .
#> c . .
#> d . .
#> e . .
#> f . .
#> g . 0.024712676
#> h . 0.330121691
#> i . 0.009822477
#> j 0.009822477 .