graph2SparseM: Coercion methods between graphs and sparse matrices

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/methods-graph.R

Description

These functions provide coercions between objects that inherit from the graph class to sparse matrices from the SparseM package.

Usage

1
2
graph2SparseM(g, useweights=FALSE)
sparseM2Graph(sM, nodeNames, edgemode=c("directed", "undirected")) 

Arguments

g

An instance of the graph class.

useweights

A logical value indicating whether to use the edge weights in the graph as values in the sparse matrix.

sM

A sparse matrix.

nodeNames

A character vector of the node names.

edgemode

Specifies whether the graph to be created should have directed (default) or undirected edges. If undirected, the input matrix sM must be symmetric.

Details

A very simple coercion from one representation to another.

Currently it is presumed that the matrix is square. For other graph formats, such as bipartite graphs, some improvements will be needed; patches are welcome.

Value

graph2SparseM takes as input an instance of a subclass of the graph class and returns a sparse matrix.

sparseM2Graph takes a sparse matrix as input and returns an instance of the graphNEL class. By default, the graphNEL returned will have directed edges.

Author(s)

R. Gentleman

See Also

graph-class, graphNEL-class, and for other conversions, aM2bpG and ftM2adjM

Examples

1
2
3
4
5
6
  set.seed(123)
  g1 <- randomGraph(letters[1:10], 1:4, p=.3)
  s1 <- graph2SparseM(g1, useweights=TRUE)
  g2 <- sparseM2Graph(s1, letters[1:10], edgemode="undirected")
  ## consistency check
  stopifnot(all.equal(g1, g2))

pshannon-bioc/graph documentation built on May 26, 2019, 10:32 a.m.