Learn R Programming

rgraph6 (version 2.0-4)

as_sparse6: Encode network data as sparse6 symbols

Description

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, ...)

Value

A character vector of 'sparse6' symbols.

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

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

Run this code
# 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)
}

Run the code above in your browser using DataLab