as_sparse6 | R Documentation |
sparse6
symbolsGeneric function encoding network data as 'sparse6' symbol(s). See below for available methods.
as_sparse6(object, ...)
## S3 method for class 'matrix'
as_sparse6(object, n = max(object, 0), ...)
## S3 method for class 'igraph'
as_sparse6(object, ...)
## S3 method for class 'network'
as_sparse6(object, ...)
## S3 method for class 'list'
as_sparse6(object, ...)
## Default S3 method:
as_sparse6(object, ...)
object |
an edgelist, igraph, or network object or a list thereof. See Methods section below. |
... |
other arguments passed to/from other methods |
n |
number of vertices in the graph |
A character vector of 'sparse6' symbols.
as_sparse6(matrix)
: Expects object
to be a two-column matrix of
integers which is interpreted as an edgelist of an undirected graph. By
default the network size is inferred to be the maximal element of object
.
This can be overridden by providing the network size via the n
argument,
the results will not be identical though (see the Examples).
as_sparse6(igraph)
: Igraph object
needs to be an undirected graph.
Requires igraph package.
as_sparse6(network)
: Network object
needs to be a directed network.
Requires network package.
as_sparse6(list)
: If object
is a list the function is applied to each
element. Consequently, it can be a list with a mixture of supported objects
classes (edgelist matrices, igraph, or network objects).
as_sparse6(default)
: The default method fails gracefully.
The 'sparse6' format is designed for undirected graphs. Error is thrown in case it is given a directed graph.
# From edgelist matrix -----------------------------------------------------
elm <- matrix(c(
1, 2,
2, 3,
3, 4
), ncol=2, byrow=TRUE)
as_sparse6(elm) # 1--2, 2--3, 3--4
as_sparse6(elm + 6) # 1, 2, 3, 4, 5, 6, 7--8, 8--9, 9--10
as_sparse6(elm, n = 10) # 1--2, 2--3, 3--4, 5, 6, 7, 8, 9, 10
# From igraph objects ------------------------------------------------------
if(requireNamespace("igraph")) {
g <- igraph::graph_from_edgelist(elm, directed=FALSE)
as_sparse6(g)
}
# From network objects --------------------------------
if(requireNamespace("network")) {
net <- network::network(elm, directed=FALSE)
as_graph6(net)
}
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.