Description Usage Arguments Value See Also Examples
The Matrix package has supported conversion from and to
"graph"
objects from (Bioconductor) package
graph since summer 2005, via the usual as(., "<class>")
coercion,
1  as(from, Class)

Since 2013, this functionality is further exposed as the
graph2T()
and T2graph()
functions (with further
arguments than just from
), which convert graphs to and from
the triplet form of sparse matrices (of class
"TsparseMatrix"
) .
1 2  graph2T(from, use.weights = )
T2graph(from, need.uniq = is_not_uniqT(from), edgemode = NULL)

from 
for 
use.weights 
logical indicating if weights should be used, i.e.,
equivalently the result will be numeric, i.e. of class

need.uniq 
a logical indicating if 
edgemode 
one of 
For graph2T()
, a sparse matrix inheriting from
"TsparseMatrix"
.
For T2graph()
an R object of class "graph"
.
Note that the CRAN package igraph also provides conversions from
and to sparse matrices (of package Matrix) via its
graph.adjacency()
and
get.adjacency()
.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18  if(isTRUE(try(require(graph)))) { ## super careful .. for "checking reasons"
n4 < LETTERS[1:4]; dns < list(n4,n4)
show(a1 < sparseMatrix(i= c(1:4), j=c(2:4,1), x = 2, dimnames=dns))
show(g1 < as(a1, "graph")) # directed
unlist(edgeWeights(g1)) # all '2'
show(a2 < sparseMatrix(i= c(1:4,4), j=c(2:4,1:2), x = TRUE, dimnames=dns))
show(g2 < as(a2, "graph")) # directed
# now if you want it undirected:
show(g3 < T2graph(as(a2,"TsparseMatrix"), edgemode="undirected"))
show(m3 < as(g3,"Matrix"))
show( graph2T(g3) ) # a "pattern Matrix" (nsTMatrix)
a. < sparseMatrix(i= 4:1, j=1:4, dimnames=list(n4,n4), giveC=FALSE) # no 'x'
show(a.) # "ngTMatrix"
show(g. < as(a., "graph"))
}

Loading required package: graph
Loading required package: BiocGenerics
Loading required package: parallel
Attaching package: 'BiocGenerics'
The following objects are masked from 'package:parallel':
clusterApply, clusterApplyLB, clusterCall, clusterEvalQ,
clusterExport, clusterMap, parApply, parCapply, parLapply,
parLapplyLB, parRapply, parSapply, parSapplyLB
The following objects are masked from 'package:Matrix':
colMeans, colSums, rowMeans, rowSums, which
The following objects are masked from 'package:stats':
IQR, mad, sd, var, xtabs
The following objects are masked from 'package:base':
Filter, Find, Map, Position, Reduce, anyDuplicated, append,
as.data.frame, basename, cbind, colMeans, colSums, colnames,
dirname, do.call, duplicated, eval, evalq, get, grep, grepl,
intersect, is.unsorted, lapply, lengths, mapply, match, mget,
order, paste, pmax, pmax.int, pmin, pmin.int, rank, rbind,
rowMeans, rowSums, rownames, sapply, setdiff, sort, table, tapply,
union, unique, unsplit, which, which.max, which.min
4 x 4 sparse Matrix of class "dgCMatrix"
A B C D
A . 2 . .
B . . 2 .
C . . . 2
D 2 . . .
A graphNEL graph with directed edges
Number of Nodes = 4
Number of Edges = 4
4 x 4 sparse Matrix of class "lgCMatrix"
A B C D
A .  . .
B . .  .
C . . . 
D   . .
A graphNEL graph with directed edges
Number of Nodes = 4
Number of Edges = 5
A graphNEL graph with undirected edges
Number of Nodes = 4
Number of Edges = 5
4 x 4 sparse Matrix of class "nsCMatrix"
A B C D
A .  . 
B  .  
C .  . 
D    .
4 x 4 sparse Matrix of class "nsTMatrix"
A B C D
A .  . 
B  .  
C .  . 
D    .
4 x 4 sparse Matrix of class "ngTMatrix"
A B C D
A . . . 
B . .  .
C .  . .
D  . . .
A graphNEL graph with undirected edges
Number of Nodes = 4
Number of Edges = 2
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.