File: matrix2Graph.Rd

package info (click to toggle)
r-bioc-graph 1.60.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,076 kB
  • sloc: ansic: 842; makefile: 12; sh: 3
file content (115 lines) | stat: -rw-r--r-- 4,048 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
\name{Coercions between matrix and graph representations}
\alias{aM2bpG}
\alias{ftM2adjM}
\alias{ftM2graphNEL}

\alias{coerce,matrix,graphNEL-method}
\alias{coerce,graphNEL,matrix-method}

\title{Coercions between matrix and graph representations}
\description{
A collection of functions and methods to convert various forms of
matrices into graph objects.
}
\usage{
aM2bpG(aM)
ftM2adjM(ft, W=NULL, V=NULL, edgemode="directed")
ftM2graphNEL(ft, W=NULL, V=NULL, edgemode="directed")
\S4method{coerce}{graphNEL,matrix}(from,to="matrix",strict=TRUE)
\S4method{coerce}{matrix,graphNEL}(from,to="graphNEL",strict=TRUE)
}
\arguments{
  \item{ft}{An nx2 matrix containing the \code{from/to}
    representation of graph edges.}
  \item{W}{An optional vector of edge weights.}
  \item{V}{An optional vector of node names.}
  \item{aM}{An affiliation matrix for a bipartite graph.}
  \item{edgemode}{Character. Specifies if the resulting graph is
    to be directed or undirected.}
  \item{from}{Object to coerce from, either of type \code{matrix} or \code{grpahNEL}}
  \item{to}{Character giving class to coerce to. Either "matrix" or
    "graphNEL".}
  \item{strict}{Strict object checking.}
}
\details{

In the functions \code{ftM2adjM} and \code{ftM2graphNEL}, a
\code{from/to} matrix \code{ft} is converted into an \code{adjacency}
matrix or a \code{graphNEL} object respectively.  In \code{ft},
the first column represents the \code{from} nodes and the
second column the \code{to} nodes.

To have unconnected nodes, use the \code{V} argument (see below).  The
\code{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 \code{from/to} matrix.
If \code{edgemode} is \code{undirected}, the edge \code{(u,v)} and
\code{(v,u)} must only be specified once.

\code{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 \code{ft}.  If it is not specified, edge weights of 1 are
assigned by default.

\code{V} is an optional vector of node names.  All elements of \code{ft}
must be contained in \code{V}, but not all names in \code{V} need to be
contained in \code{ft}.  If \code{V} is not specified, it is set to all
nodes represented in \code{ft}.  Specifying \code{V} is most useful for
creating a graph that includes nodes with degree 0.

\code{aM} is an affiliation matrix as frequently used in social networks
analysis.  The rows of \code{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.  \code{aM2bpG} returns a \code{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 \code{Rgraphviz} and the \code{dot} layout, the bipartite structure of
the graph returned by \code{aM2bpG} should be evident. 

An \code{adjacency} matrix can be coerced into a \code{graphNEL} using
the \code{as} method.  If the matrix is a symmetric matrix, then the
resulting graph will be \code{undirected}, otherwise it will be
\code{directed}.
}

\value{
For \code{ftM2graphNEL} and \code{aM2bpG}, an object of class
\code{graphNEL}.
For \code{ftM2adjM}, a matrix (the adjacency matrix representation).
}

\author{Denise Scholtens, Wolfgang Huber}

\examples{

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


}

\keyword{graphs }