Make an igraph object from various input types.
to_igraph(x, bipartite = FALSE, vertices = NULL)
input object
logical, whether an adjacency matrix represents a bipartite
network. Forces the creation of a bipartite igraph x. This argument is
only used when a matrix is converted to an igraph
object and is
ignored otherwise.
A data frame with vertex metadata, or NULL
. See details below.
The following inputs are supported:
network
object created by the network package
This is a base R type matrix. When a matrix is used as input,
the function the number of rows is equal to the number of columns.
If they do not, the function assumes the matrix refers to a bipartite network.
This assumption can be overridden by the bipartite
argument.
The data.frame
contains an edge list.
The data.frame requires the first column to contain the senders and the
second column contains the receivers. If there are any additional
columns, these are considered to be edge attributes.
NOTE: The created igraph
object is considered to be directed,
depending on the structure of the input.
If an undirected network is required,
run as.undirected
on the output from this function.
When a matrix is used as input, the function the number of rows is equal
to the number of columns. If they do not, the function assumes the matrix
refers to a bipartite network. This assumption can be overridden by
the bipartite
argument.
The vertices
argument is only used when x
is a
data.frame
and is ignored otherwise.
If vertices
is NULL, then the first two columns of x
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 x
is checked to contain only
vertex names listed in vertices.
See graph_from_data_frame
for the underlying function.
The functions are largely based upon as_igraph
functions from
the migraph
package.
The versions in the snafun
package do not require
the tidyverse dependencies of migraph
and do not deal with tidygraph
.
# from a matrix
g <- igraph::sample_gnp(10, 2/10)
mat <- igraph::as_adjacency_matrix(g, sparse = FALSE)
to_igraph(mat)
#> IGRAPH e93fb99 U--- 10 8 --
#> + edges from e93fb99:
#> [1] 1--6 1--8 2--3 2--6 3--4 3--7 5--6 8--9
g <- igraph::make_ring(10)
igraph::E(g)$weight <- seq_len(igraph::ecount(g))
mat <- igraph::as_adjacency_matrix(g, sparse = FALSE, attr = "weight")
to_igraph(mat)
#> IGRAPH e941c94 U-W- 10 10 --
#> + attr: weight (e/n)
#> + edges from e941c94:
#> [1] 1-- 2 1--10 2-- 3 3-- 4 4-- 5 5-- 6 6-- 7 7-- 8 8-- 9 9--10
# bipartite network, even nodes are one type, odd vertices another type
g <- igraph::make_bipartite_graph( rep(0:1,length=10), c(1:10))
mat <- igraph::as_adjacency_matrix(g, sparse = FALSE)
to_igraph(mat) # same network, but not officially bipartite
#> IGRAPH e9439a2 U--- 10 5 --
#> + edges from e9439a2:
#> [1] 1-- 2 3-- 4 5-- 6 7-- 8 9--10
mat <- igraph::as_biadjacency_matrix(g, sparse = FALSE)
to_igraph(mat, bipartite = TRUE)
#> IGRAPH e9452c2 UN-B 10 5 --
#> + attr: type (v/l), name (v/c)
#> + edges from e9452c2 (vertex names):
#> [1] 1--2 3--4 5--6 7--8 9--10
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))
to_igraph(relations)
#> IGRAPH e946641 DN-- 5 6 --
#> + attr: name (v/c), same.dept (e/l), friendship (e/n), advice (e/n)
#> + edges from e946641 (vertex names):
#> [1] Bob ->Alice Cecil ->Bob Cecil ->Alice David ->Alice
#> [5] David ->Bob Esmeralda->Alice
if (FALSE) { # \dontrun{
aa <- data.frame(from = c(1,1,2,2,3,3,4,4),
to = c(11, 12, 13, 14, 15, 16, 17, 18))
to_igraph(aa) # message is given if this should ne bipartite
to_igraph(aa, bipartite = TRUE)
} # }