Coercions between matrix and graph representations
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") "coerce"(from,to="matrix",strict=TRUE) "coerce"(from,to="graphNEL",strict=TRUE)
- An nx2 matrix containing the
from/torepresentation of graph edges.
- An optional vector of edge weights.
- An optional vector of node names.
- An affiliation matrix for a bipartite graph.
- Character. Specifies if the resulting graph is to be directed or undirected.
- Object to coerce from, either of type
- Character giving class to coerce to. Either "matrix" or "graphNEL".
- Strict object checking.
In the functions
ft is converted into an
matrix or a
graphNEL object respectively. In
the first column represents the
from nodes and the
second column the
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
undirected, the edge
(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
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
must be contained in
V, but not all names in
V need to be
V is not specified, it is set to all
nodes represented in
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
Rgraphviz and the
dot layout, the bipartite structure of
the graph returned by
aM2bpG should be evident.
adjacency matrix can be coerced into a
as method. If the matrix is a symmetric matrix, then the
resulting graph will be
undirected, otherwise it will be
aM2bpG, an object of class
ftM2adjM, a matrix (the adjacency matrix representation).
## 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))