as_matrix() coerces the object into an adjacency (one-mode/unipartite) or incidence (two-mode/bipartite) matrix.

If the network is a cognitive social structure or an egocentric network (i.e. the edgelist contains a 'by' column naming who reported each tie), or a gossip network (with an 'about' column naming whom each tie is about), as_matrix() returns a three-dimensional array instead, with dimensions for senders, receivers, and reporters (or targets). Every node takes a row, a column, and a slice, in the order of the nodes, and a tie recorded as missing holds NA. Where such a network has several layers, a list of one array for each layer is returned.

Where a network holds parallel ties, i.e. where tie_is_parallel() is TRUE for any tie, the cells of the matrix report how many ties join each pair of nodes, and so may be greater than one even where the network is neither weighted nor signed.

as_matrix() also turns a node measure or a node membership, as the node_*() functions and node_attribute() return them, into a matrix of the pairs of nodes, compared as compare says. This gives the dyadic covariates of an MRQAP model, such as those migraph::net_regression() builds.

This coercion is extractive in the sense that it loses any information that a matrix cannot hold, such as tie attributes other than the weight, or the direction of a two-mode network.

as_matrix(.data, twomode = NULL, ...)

# S3 method for class 'node_measure'
as_matrix(
  .data,
  twomode = NULL,
  compare = c("absdiff", "diff", "sender", "receiver"),
  ...
)

# S3 method for class 'node_member'
as_matrix(.data, twomode = NULL, compare = "same", ...)

Arguments

.data

An object of a {manynet}-consistent class:

  • adjacency or incidence matrix from {base} R

  • edgelist data.frame from {base} R or tbl/tbl_df from {tibble}

  • stocnet stocnet, from the {manynet} package

  • igraph igraph, from the {igraph} package

  • network network, from the {network} package

  • tidygraph tbl_graph, from the {tidygraph} package

twomode

Logical option used to override heuristics for distinguishing incidence (two-mode/bipartite) from adjacency (one-mode/unipartite) networks. By default FALSE.

...

Other arguments passed to or from other methods.

compare

How the values of two nodes are compared in each cell of the matrix returned for a node measure or a node membership. For a node measure x:

  • "absdiff" (the default) gives abs(x[i] - x[j]),

  • "diff" gives x[i] - x[j], which t() turns into x[j] - x[i],

  • "sender" gives x[i], the value of the node in the row,

  • "receiver" gives x[j], the value of the node in the column.

For a node membership, "same" gives 1 where the two nodes belong to the same group and 0 where they do not. The diagonal of a one-mode result is 0, and a missing value gives a missing cell. Where the vector comes from a two-mode network, the rows are the nodes of the first mode and the columns the nodes of the second.

Value

The currently implemented coercions or translations are:

          data.frame diff_model igraph matrix network network.goldfish
as_matrix          *          *      *      *       *                *
          node_measure node_member siena stocnet tbl_graph
as_matrix            *           *     *       *         *

Details

Matrices can be either adjacency (one-mode) or incidence (two-mode) matrices. Incidence matrices are typically inferred from unequal dimensions, but since in rare cases a matrix with equal dimensions may still be an incidence matrix, an additional argument twomode can be specified to override this heuristic.

See also

Other coercions: coerce_graph, coerce_list

Examples

test <- data.frame(from = c("A","B","B","C","C"), to = c("I","G","I","G","H"))
as_matrix(test)
#>   G H I
#> A 0 0 1
#> B 1 0 1
#> C 1 1 0
as_matrix(node_attribute(ison_lawfirm, "age"))[1:5, 1:5]
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    2    3    5    5
#> [2,]    2    0    5    3    3
#> [3,]    3    5    0    8    8
#> [4,]    5    3    8    0    0
#> [5,]    5    3    8    0    0
as_matrix(node_attribute(ison_lawfirm, "age"), compare = "diff")[1:5, 1:5]
#>      [,1] [,2] [,3] [,4] [,5]
#> [1,]    0    2   -3    5    5
#> [2,]   -2    0   -5    3    3
#> [3,]    3    5    0    8    8
#> [4,]   -5   -3   -8    0    0
#> [5,]   -5   -3   -8    0    0