as_sparse6: Encode network data as 'sparse6' symbols

View source: R/as_sparse6.R

as_sparse6R Documentation

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

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
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)
}


mbojan/rgraph6 documentation built on Sept. 25, 2024, 2:14 a.m.