Become an expert in R — Interactive courses, Cheat Sheets, certificates and more!
Get Started for Free

as.matrix.network

Coerce a Network Object to Matrix or Table Form


Description

The as.matrix methods attempt to coerce their input to a matrix in adjacency, incidence, or edgelist form. Edge values (from a stored attribute) may be used if present. as_tibble coerces into an edgelist in tibble (a type of data.frame) form; this can be especially useful if extrecting a character-type edge attribute.

Usage

## S3 method for class 'network'
as.matrix(x, matrix.type = NULL, attrname = NULL,
  ...)

## S3 method for class 'adjacency'
as.matrix.network(x, attrname=NULL, 
   expand.bipartite = FALSE, ...)

## S3 method for class 'edgelist'
as.matrix.network(x, attrname=NULL, 
   as.sna.edgelist = FALSE, na.rm = TRUE, ...)

## S3 method for class 'network'
as_tibble(x, attrnames = (match.arg(unit) ==
  "vertices"), na.rm = TRUE, ..., unit = c("edges", "vertices"))

## S3 method for class 'network'
as.tibble(x, attrnames = (match.arg(unit)=="vertices"), na.rm = TRUE, ...,
   unit=c("edges","vertices"))

## S3 method for class 'incidence'
as.matrix.network(x, attrname=NULL, ...)

Arguments

x

an object of class network

matrix.type

one of "adjacency", "incidence", "edgelist", or NULL

attrname

optionally, the name of an edge attribute to use for edge values

...

additional arguments.

expand.bipartite

logical; if x is bipartite, should we return the full adjacency matrix (rather than the abbreviated, two-mode form)?

as.sna.edgelist

logical; should the edgelist be returned in sna edglist form?

na.rm

logical; should missing edges/vertices be included in the edgelist formats? Ignored if as.sna.edgelist=TRUE.

attrnames

optionally, either a character vector of the names of edge attributes to use for edge values, or a numerical or logical vector to use as indices for selecting them from list.edge.attributes(x) or list.vertex.attributes(x) (depending on unit); passing TRUE therefore returns all edge attributes as columns

unit

whether a tibble of edge or vertex attributes should be returned.

Details

If no matrix type is specified, which.matrix.type will be used to make an educated guess based on the shape of x. Where edge values are not specified, a dichotomous matrix will be assumed.

Edgelists returned by the as.matrix methods are by default in a slightly different form from the sna edgelist standard, but do contain the sna extended matrix attributes (see as.network.matrix). They should typically be compatible with sna library functions. To ensure compatibility, the as.sna.edgelist argument can be set (which returns an exact sna edgelist). The as.edgelist function also returns a similar edgelist matrix but with an enforced sorting.

For the as.matrix methods, if the attrname attribute is used to include a charcter attribute, the resulting edgelist matrix will be character rather than numeric. The as_tibble methods never coerce.

Note that adjacency matrices may also be obtained using the extraction operator. See the relevant man page for details. Also note that which attributes get returned by the as_tibble method by default depends on unit: by default no edge attributes are returned but all vertex attributes are.

Value

For as.matrix methods, an adjacency, incidence, or edgelist matrix. For the as_tibble method, a tibble whose first two columns are .head and .tail, whose third column .eid is the edge ID, and whose subsequent columns are the requested edge attributes.

Author(s)

Carter T. Butts buttsc@uci.edu and David Hunter dhunter@stat.psu.edu

References

Butts, C. T. (2008). “network: a Package for Managing Relational Data in R.” Journal of Statistical Software, 24(2). https://www.jstatsoft.org/v24/i02/

See Also

Examples

# Create a random network
m <- matrix(rbinom(25,4,0.159),5,5) # 50% density
diag(m) <- 0
g <- network(m, ignore.eval=FALSE, names.eval="a") # With values
g %e% "ac" <- letters[g %e% "a"]

# Coerce to matrix form
# No attributes:
as.matrix(g,matrix.type="adjacency")
as.matrix(g,matrix.type="incidence")
as.matrix(g,matrix.type="edgelist")
# Attributes:
as.matrix(g,matrix.type="adjacency",attrname="a")
as.matrix(g,matrix.type="incidence",attrname="a")
as.matrix(g,matrix.type="edgelist",attrname="a")
as.matrix(g,matrix.type="edgelist",attrname="ac")

# Coerce to a tibble:
library(tibble)
as_tibble(g)
as_tibble(g, attrnames=c("a","ac"))
as_tibble(g, attrnames=TRUE)
# Get vertex attributes instead:
as_tibble(g, unit = "vertices")

# Missing data handling:
g[1,2] <- NA
as.matrix(g,matrix.type="adjacency") # NA in the corresponding cell
as.matrix(g,matrix.type="edgelist", na.rm=TRUE) # (1,2) excluded
as.matrix(g,matrix.type="edgelist", na.rm=FALSE) # (1,2) included
as_tibble(g, attrnames="na", na.rm=FALSE) # Which edges are marked missing?

# Can also use the extraction operator
g[,]                            # Get entire adjacency matrix
g[1:2,3:5]                      # Obtain a submatrix

network

Classes for Relational Data

v1.16.1
GPL (>= 2)
Authors
Carter T. Butts [aut, cre], David Hunter [ctb], Mark Handcock [ctb], Skye Bender-deMoll [ctb], Jeffrey Horner [ctb], Li Wang [ctb]
Initial release
2020-10-06

We don't support your browser anymore

Please choose more modern alternatives, such as Google Chrome or Mozilla Firefox.