as_data_frame | R Documentation |
This function creates an igraph graph from one or two data frames containing the (symbolic) edge list and edge/vertex attributes.
as_data_frame(x, what = c("edges", "vertices", "both"))
graph_from_data_frame(d, directed = TRUE, vertices = NULL)
from_data_frame(...)
x |
An igraph object. |
what |
Character constant, whether to return info about vertices, edges, or both. The default is ‘edges’. |
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
|
directed |
Logical scalar, whether or not to create a directed graph. |
vertices |
A data frame with vertex metadata, or |
... |
Passed to |
graph_from_data_frame()
creates igraph graphs from one or two data frames.
It has two modes of operation, depending whether the vertices
argument is NULL
or not.
If vertices
is NULL
, then the first two columns of 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 vertices
is not NULL
, then it must be a data frame giving
vertex metadata. The first column of vertices
is assumed to contain
symbolic vertex names, this will be added to the graphs as the
‘name
’ vertex attribute. Other columns will be added as
additional vertex attributes. If vertices
is not NULL
then the
symbolic edge list given in d
is checked to contain only vertex names
listed in vertices
.
Typically, the data frames are exported from some spreadsheet software like
Excel and are imported into R via read.table()
,
read.delim()
or read.csv()
.
All edges in the data frame are included in the graph, which may include multiple parallel edges and loops.
as_data_frame()
converts the igraph graph into one or more data
frames, depending on the what
argument.
If the what
argument is 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 from
and 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 from
or 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 what
argument is vertices
, then vertex attributes are
returned. Vertices are listed in the order of their numeric vertex ids.
If the what
argument is both
, then both vertex and edge data
is returned, in a list with named entries vertices
and edges
.
An igraph graph object for graph_from_data_frame()
, and either a
data frame or a list of two data frames named edges
and
vertices
for as.data.frame
.
For graph_from_data_frame()
NA
elements in the first two
columns ‘d’ are replaced by the string “NA” before creating
the graph. This means that all NA
s will correspond to a single
vertex.
NA
elements in the first column of ‘vertices’ are also
replaced by the string “NA”, but the rest of ‘vertices’ is not
touched. In other words, vertex names (=the first column) cannot be
NA
, but other vertex attributes can.
Gabor Csardi csardi.gabor@gmail.com
graph_from_literal()
for another way to create graphs, read.table()
to read in tables
from files.
Other conversion:
as.matrix.igraph()
,
as_adj_list()
,
as_adjacency_matrix()
,
as_biadjacency_matrix()
,
as_directed()
,
as_edgelist()
,
as_graphnel()
,
as_long_data_frame()
,
graph_from_adj_list()
,
graph_from_graphnel()
Other biadjacency:
graph_from_biadjacency_matrix()
## 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")
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.