Coercions between matrix and graph representations | R Documentation |
A collection of functions and methods to convert various forms of matrices into graph objects.
aM2bpG(aM)
ftM2adjM(ft, W=NULL, V=NULL, edgemode="directed")
ftM2graphNEL(ft, W=NULL, V=NULL, edgemode="directed")
## S4 method for signature 'graphNEL,matrix'
coerce(from,to="matrix",strict=TRUE)
## S4 method for signature 'matrix,graphNEL'
coerce(from,to="graphNEL",strict=TRUE)
ft |
An nx2 matrix containing the |
W |
An optional vector of edge weights. |
V |
An optional vector of node names. |
aM |
An affiliation matrix for a bipartite graph. |
edgemode |
Character. Specifies if the resulting graph is to be directed or undirected. |
from |
Object to coerce from, either of type |
to |
Character giving class to coerce to. Either "matrix" or "graphNEL". |
strict |
Strict object checking. |
In the functions ftM2adjM
and ftM2graphNEL
, a
from/to
matrix ft
is converted into an adjacency
matrix or a graphNEL
object respectively. In ft
,
the first column represents the from
nodes and the
second column the to
nodes.
To have unconnected nodes, use the V
argument (see below). The
edgemode
parameter can be used to specify if the desired output
is a directed or undirected graph.
The same edge must not occur twice in the from/to
matrix.
If edgemode
is undirected
, the edge (u,v)
and
(v,u)
must only be specified once.
W
is an optional vector of edge weights. The order of the edge
weights in the vector should correspond to the order of the edges
recorded in ft
. If it is not specified, edge weights of 1 are
assigned by default.
V
is an optional vector of node names. All elements of ft
must be contained in V
, but not all names in V
need to be
contained in ft
. If V
is not specified, it is set to all
nodes represented in ft
. Specifying V
is most useful for
creating a graph that includes nodes with degree 0.
aM
is an affiliation matrix as frequently used in social networks
analysis. The rows of aM
represent actors, and the columns
represent events. An entry of "1" in the ith row and jth column
represents affiliation of the ith actor with the jth event. Weighted
entries may also be used. aM2bpG
returns a graphNEL
object with
nodes consisting of the set of actors and events, and directed (possibly
weighted) edges from the actors to their corresponding events. If
plotted using Rgraphviz
and the dot
layout, the bipartite structure of
the graph returned by aM2bpG
should be evident.
An adjacency
matrix can be coerced into a graphNEL
using
the as
method. If the matrix is a symmetric matrix, then the
resulting graph will be undirected
, otherwise it will be
directed
.
For ftM2graphNEL
and aM2bpG
, an object of class
graphNEL
.
For ftM2adjM
, a matrix (the adjacency matrix representation).
Denise Scholtens, Wolfgang Huber
## From-To matrix
From <- c("A","A","C","C")
To <- c("B","C","B","D")
L <- cbind(From,To)
W <- 1:4
M1 <- ftM2adjM(L, W, edgemode="directed")
M2 <- ftM2adjM(L, W, edgemode="undirected")
stopifnot(all(M1+t(M1)==M2))
G1 <- ftM2graphNEL(L, W, edgemode="directed")
G2 <- ftM2graphNEL(L, W, edgemode="undirected")
## Adjacency matrix
From <- matrix(runif(100), nrow=10, ncol=10)
From <- (From+t(From)) > pi/4
rownames(From) <- colnames(From) <- LETTERS[1:10]
To <- as(From,"graphNEL")
Back <- as(To,"matrix")
stopifnot(all(From == Back))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.