as.matrix.network.RdThe 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.
# 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"),
store.eid = FALSE
)
as.tibble.network(
x,
attrnames = (match.arg(unit) == "vertices"),
na.rm = TRUE,
...,
unit = c("edges", "vertices"),
store.eid = FALSE
)
# S3 method for class 'incidence'
as.matrix.network(x, attrname=NULL, ...)an object of class network
one of "adjacency", "incidence",
"edgelist", or NULL
optionally, the name of an edge attribute to use for edge values
additional arguments.
logical; if x is bipartite, should we return
the full adjacency matrix (rather than the abbreviated, two-mode form)?
logical; should the edgelist be returned in sna edglist form?
logical; should missing edges/vertices be included in the
edgelist formats? Ignored if as.sna.edgelist=TRUE.
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
whether a tibble of edge or vertex attributes
should be returned.
whether the edge ID should be stored in the third column (.eid).
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.
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.
Butts, C. T. (2008). “network: a Package for Managing Relational Data in R.” Journal of Statistical Software, 24(2). doi:10.18637/jss.v024.i02
# 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")
#> 1 2 3 4 5
#> 1 0 0 0 1 0
#> 2 0 0 1 0 0
#> 3 0 0 0 1 0
#> 4 0 0 0 0 1
#> 5 0 1 1 1 0
as.matrix(g,matrix.type="incidence")
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,] 0 0 0 -1 0 0 0
#> [2,] 1 -1 0 0 0 0 0
#> [3,] 0 1 1 0 -1 0 0
#> [4,] 0 0 0 1 1 1 -1
#> [5,] -1 0 -1 0 0 -1 1
as.matrix(g,matrix.type="edgelist")
#> [,1] [,2]
#> [1,] 5 2
#> [2,] 2 3
#> [3,] 5 3
#> [4,] 1 4
#> [5,] 3 4
#> [6,] 5 4
#> [7,] 4 5
#> attr(,"n")
#> [1] 5
#> attr(,"vnames")
#> [1] 1 2 3 4 5
# Attributes:
as.matrix(g,matrix.type="adjacency",attrname="a")
#> 1 2 3 4 5
#> 1 0 0 0 1 0
#> 2 0 0 1 0 0
#> 3 0 0 0 1 0
#> 4 0 0 0 0 1
#> 5 0 2 2 1 0
as.matrix(g,matrix.type="incidence",attrname="a")
#> [,1] [,2] [,3] [,4] [,5] [,6] [,7]
#> [1,] 0 0 0 -1 0 0 0
#> [2,] 2 -1 0 0 0 0 0
#> [3,] 0 1 2 0 -1 0 0
#> [4,] 0 0 0 1 1 1 -1
#> [5,] -2 0 -2 0 0 -1 1
as.matrix(g,matrix.type="edgelist",attrname="a")
#> [,1] [,2] [,3]
#> [1,] 5 2 2
#> [2,] 2 3 1
#> [3,] 5 3 2
#> [4,] 1 4 1
#> [5,] 3 4 1
#> [6,] 5 4 1
#> [7,] 4 5 1
#> attr(,"n")
#> [1] 5
#> attr(,"vnames")
#> [1] 1 2 3 4 5
as.matrix(g,matrix.type="edgelist",attrname="ac")
#> [,1] [,2] [,3]
#> [1,] "5" "2" "b"
#> [2,] "2" "3" "a"
#> [3,] "5" "3" "b"
#> [4,] "1" "4" "a"
#> [5,] "3" "4" "a"
#> [6,] "5" "4" "a"
#> [7,] "4" "5" "a"
#> attr(,"n")
#> [1] 5
#> attr(,"vnames")
#> [1] 1 2 3 4 5
# Coerce to a tibble:
library(tibble)
as_tibble(g)
#> # A tibble: 7 × 2
#> .tail .head
#> * <int> <int>
#> 1 5 2
#> 2 2 3
#> 3 5 3
#> 4 1 4
#> 5 3 4
#> 6 5 4
#> 7 4 5
as_tibble(g, attrnames=c("a","ac"))
#> # A tibble: 7 × 4
#> .tail .head a ac
#> * <int> <int> <dbl> <chr>
#> 1 5 2 2 b
#> 2 2 3 1 a
#> 3 5 3 2 b
#> 4 1 4 1 a
#> 5 3 4 1 a
#> 6 5 4 1 a
#> 7 4 5 1 a
as_tibble(g, attrnames=TRUE)
#> # A tibble: 7 × 5
#> .tail .head a ac na
#> * <int> <int> <dbl> <chr> <lgl>
#> 1 5 2 2 b FALSE
#> 2 2 3 1 a FALSE
#> 3 5 3 2 b FALSE
#> 4 1 4 1 a FALSE
#> 5 3 4 1 a FALSE
#> 6 5 4 1 a FALSE
#> 7 4 5 1 a FALSE
# Get vertex attributes instead:
as_tibble(g, unit = "vertices")
#> # A tibble: 5 × 2
#> vertex.names na
#> * <int> <lgl>
#> 1 1 FALSE
#> 2 2 FALSE
#> 3 3 FALSE
#> 4 4 FALSE
#> 5 5 FALSE
# Missing data handling:
g[1,2] <- NA
as.matrix(g,matrix.type="adjacency") # NA in the corresponding cell
#> 1 2 3 4 5
#> 1 0 NA 0 1 0
#> 2 0 0 1 0 0
#> 3 0 0 0 1 0
#> 4 0 0 0 0 1
#> 5 0 1 1 1 0
as.matrix(g,matrix.type="edgelist", na.rm=TRUE) # (1,2) excluded
#> [,1] [,2]
#> [1,] 5 2
#> [2,] 2 3
#> [3,] 5 3
#> [4,] 1 4
#> [5,] 3 4
#> [6,] 5 4
#> [7,] 4 5
#> attr(,"n")
#> [1] 5
#> attr(,"vnames")
#> [1] 1 2 3 4 5
as.matrix(g,matrix.type="edgelist", na.rm=FALSE) # (1,2) included
#> [,1] [,2]
#> [1,] 5 2
#> [2,] 2 3
#> [3,] 5 3
#> [4,] 1 4
#> [5,] 3 4
#> [6,] 5 4
#> [7,] 4 5
#> [8,] 1 2
#> attr(,"n")
#> [1] 5
#> attr(,"vnames")
#> [1] 1 2 3 4 5
as_tibble(g, attrnames="na", na.rm=FALSE) # Which edges are marked missing?
#> # A tibble: 8 × 3
#> .tail .head na
#> * <int> <int> <lgl>
#> 1 5 2 FALSE
#> 2 2 3 FALSE
#> 3 5 3 FALSE
#> 4 1 4 FALSE
#> 5 3 4 FALSE
#> 6 5 4 FALSE
#> 7 4 5 FALSE
#> 8 1 2 TRUE
# Can also use the extraction operator
g[,] # Get entire adjacency matrix
#> 1 2 3 4 5
#> 1 0 NA 0 1 0
#> 2 0 0 1 0 0
#> 3 0 0 0 1 0
#> 4 0 0 0 0 1
#> 5 0 1 1 1 0
g[1:2,3:5] # Obtain a submatrix
#> 3 4 5
#> 1 0 1 0
#> 2 1 0 0