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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151
|
% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/conversion.R, R/data_frame.R
\name{as_data_frame}
\alias{as_data_frame}
\alias{graph_from_data_frame}
\alias{from_data_frame}
\title{Creating igraph graphs from data frames or vice-versa}
\usage{
as_data_frame(x, what = c("edges", "vertices", "both"))
graph_from_data_frame(d, directed = TRUE, vertices = NULL)
from_data_frame(...)
}
\arguments{
\item{x}{An igraph object.}
\item{what}{Character constant, whether to return info about vertices,
edges, or both. The default is \sQuote{edges}.}
\item{d}{A data frame containing a symbolic edge list in the first two
columns. Additional columns are considered as edge attributes. Since
version 0.7 this argument is coerced to a data frame with
\code{as.data.frame}.}
\item{directed}{Logical scalar, whether or not to create a directed graph.}
\item{vertices}{A data frame with vertex metadata, or \code{NULL}. See
details below. Since version 0.7 this argument is coerced to a data frame
with \code{as.data.frame}, if not \code{NULL}.}
\item{...}{Passed to \code{graph_from_data_frame()}.}
}
\value{
An igraph graph object for \code{graph_from_data_frame()}, and either a
data frame or a list of two data frames named \code{edges} and
\code{vertices} for \code{as.data.frame}.
}
\description{
This function creates an igraph graph from one or two data frames containing
the (symbolic) edge list and edge/vertex attributes.
}
\details{
\code{graph_from_data_frame()} creates igraph graphs from one or two data frames.
It has two modes of operation, depending whether the \code{vertices}
argument is \code{NULL} or not.
If \code{vertices} is \code{NULL}, then the first two columns of \code{d}
are used as a symbolic edge list and additional columns as edge attributes.
The names of the attributes are taken from the names of the columns.
If \code{vertices} is not \code{NULL}, then it must be a data frame giving
vertex metadata. The first column of \code{vertices} is assumed to contain
symbolic vertex names, this will be added to the graphs as the
\sQuote{\code{name}} vertex attribute. Other columns will be added as
additional vertex attributes. If \code{vertices} is not \code{NULL} then the
symbolic edge list given in \code{d} is checked to contain only vertex names
listed in \code{vertices}.
Typically, the data frames are exported from some spreadsheet software like
Excel and are imported into R via \code{\link[=read.table]{read.table()}},
\code{\link[=read.delim]{read.delim()}} or \code{\link[=read.csv]{read.csv()}}.
All edges in the data frame are included in the graph, which may include
multiple parallel edges and loops.
\code{as_data_frame()} converts the igraph graph into one or more data
frames, depending on the \code{what} argument.
If the \code{what} argument is \code{edges} (the default), then the edges of
the graph and also the edge attributes are returned. The edges will be in
the first two columns, named \code{from} and \code{to}. (This also denotes
edge direction for directed graphs.) For named graphs, the vertex names
will be included in these columns, for other graphs, the numeric vertex ids.
The edge attributes will be in the other columns. It is not a good idea to
have an edge attribute named \code{from} or \code{to}, because then the
column named in the data frame will not be unique. The edges are listed in
the order of their numeric ids.
If the \code{what} argument is \code{vertices}, then vertex attributes are
returned. Vertices are listed in the order of their numeric vertex ids.
If the \code{what} argument is \code{both}, then both vertex and edge data
is returned, in a list with named entries \code{vertices} and \code{edges}.
}
\note{
For \code{graph_from_data_frame()} \code{NA} elements in the first two
columns \sQuote{d} are replaced by the string \dQuote{NA} before creating
the graph. This means that all \code{NA}s will correspond to a single
vertex.
\code{NA} elements in the first column of \sQuote{vertices} are also
replaced by the string \dQuote{NA}, but the rest of \sQuote{vertices} is not
touched. In other words, vertex names (=the first column) cannot be
\code{NA}, but other vertex attributes can.
}
\examples{
## A simple example with a couple of actors
## The typical case is that these tables are read in from files....
actors <- data.frame(
name = c(
"Alice", "Bob", "Cecil", "David",
"Esmeralda"
),
age = c(48, 33, 45, 34, 21),
gender = c("F", "M", "F", "M", "F")
)
relations <- data.frame(
from = c(
"Bob", "Cecil", "Cecil", "David",
"David", "Esmeralda"
),
to = c("Alice", "Bob", "Alice", "Alice", "Bob", "Alice"),
same.dept = c(FALSE, FALSE, TRUE, FALSE, FALSE, TRUE),
friendship = c(4, 5, 5, 2, 1, 1), advice = c(4, 5, 5, 4, 2, 3)
)
g <- graph_from_data_frame(relations, directed = TRUE, vertices = actors)
print(g, e = TRUE, v = TRUE)
## The opposite operation
as_data_frame(g, what = "vertices")
as_data_frame(g, what = "edges")
}
\seealso{
\code{\link[=graph_from_literal]{graph_from_literal()}}
for another way to create graphs, \code{\link[=read.table]{read.table()}} to read in tables
from files.
Other conversion:
\code{\link{as.matrix.igraph}()},
\code{\link{as_adj_list}()},
\code{\link{as_adjacency_matrix}()},
\code{\link{as_biadjacency_matrix}()},
\code{\link{as_directed}()},
\code{\link{as_edgelist}()},
\code{\link{as_graphnel}()},
\code{\link{as_long_data_frame}()},
\code{\link{graph_from_adj_list}()},
\code{\link{graph_from_graphnel}()}
Other biadjacency:
\code{\link{graph_from_biadjacency_matrix}()}
}
\author{
Gabor Csardi \email{csardi.gabor@gmail.com}
}
\concept{biadjacency}
\concept{conversion}
\keyword{graphs}
|