Skip to contents

Generic function encoding network data as 'sparse6' symbol(s). See below for available methods.

Usage

as_sparse6(object, ...)

# S3 method for matrix
as_sparse6(object, n = max(object, 0), ...)

# S3 method for igraph
as_sparse6(object, ...)

# S3 method for network
as_sparse6(object, ...)

# S3 method for list
as_sparse6(object, ...)

# S3 method for default
as_sparse6(object, ...)

Arguments

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

Value

A character vector of 'sparse6' symbols.

Methods (by class)

  • 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.

See also

The 'sparse6' format is designed for undirected graphs. Error is thrown in case it is given a directed graph.

Examples

# 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
#> [1] ":Cdv"
as_sparse6(elm + 6) # 1, 2, 3, 4, 5, 6, 7--8, 8--9, 9--10
#> [1] ":ImY}N"
as_sparse6(elm, n = 10) # 1--2, 2--3, 3--4, 5, 6, 7, 8, 9, 10
#> [1] ":I`EV"

# From igraph objects ------------------------------------------------------
if(requireNamespace("igraph")) {
  g <- igraph::graph_from_edgelist(elm, directed=FALSE)
  as_sparse6(g)
}
#> [1] ":Cdv"

# From network objects --------------------------------
if(requireNamespace("network")) {
  net <- network::network(elm, directed=FALSE)
  as_graph6(net)
}
#> [1] "Ch"