network (version 1.13.0)

as.matrix.network: Coerce a Network Object to Matrix 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.

Usage

"as.matrix"(x, matrix.type = NULL, attrname = NULL, ...) "as.matrix.network"(x, attrname=NULL, expand.bipartite = FALSE, ...) "as.matrix.network"(x, attrname=NULL, as.sna.edgelist = FALSE, ...) "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
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?
...
additional arguments.

Value

An adjacency, incidence, or edgelist matrix

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 these 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. If the attrname attribute is used to include a charcter attribute, the resulting edgelist matrix will be character rather than numeric. Note that adjacency matrices may also be obtained using the extraction operator. See the relevant man page for details.

References

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

See Also

which.matrix.type, network, network.extraction,as.edgelist

Examples

Run this code
#Create a random network
m <- matrix(rbinom(25,1,0.5),5,5)
diag(m) <- 0
g <- network(m)

#Coerce to matrix form
as.matrix.network(g,matrix.type="adjacency")
as.matrix.network(g,matrix.type="incidence")
as.matrix.network(g,matrix.type="edgelist")

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

Run the code above in your browser using DataLab