The as_ functions in {migraph} coerce objects between several common classes of social network objects. These include:

  • edgelists, as data frames or tibbles

  • adjacency (one-mode/unipartite) and incidence (two-mode/bipartite) matrices

  • {igraph} graph objects

  • {tidygraph} tbl_graph objects

  • {network} network objects

An effort is made for all of these coercion routines to be as lossless as possible, though some object classes are better at retaining certain kinds of information than others. Note also that there are some reserved column names in one or more object classes, which could otherwise lead to some unexpected results.

as_edgelist(object, twomode = FALSE)

as_matrix(object, twomode = FALSE)

as_igraph(object, twomode = FALSE)

as_tidygraph(object, twomode = FALSE)

as_network(object, twomode = FALSE)

Arguments

object

An object of a migraph-consistent class:

  • matrix (adjacency or incidence) from {base} R

  • edgelist, a data frame from {base} R or tibble from {tibble}

  • igraph, from the {igraph} package

  • network, from the {network} package

  • tbl_graph, from the {tidygraph} package

twomode

Logical option used to override heuristics for distinguishing incidence (two-mode/bipartite) from adjacency (one-mode/unipartite) networks. By default FALSE.

Value

The currently implemented coercions or translations are:

to/fromedgelistsmatricesigraphtidygraphnetworkgoldfish
edgelists (data frames)XXXXXX
matricesXXXXXX
igraphXXXXXX
tidygraphXXXXXX
networkXXXXXX

Details

Edgelists are expected to be held in data.frame or tibble class objects. The first two columns of such an object are expected to be the senders and receivers of a tie, respectively, and are typically named "from" and "to" (even in the case of an undirected network). These columns can contain integers to identify nodes or character strings/factors if the network is labelled. If the sets of senders and receivers overlap, a one-mode network is inferred. If the sets contain no overlap, a two-mode network is inferred. If a third, numeric column is present, a weighted network will be created.

Matrices can be either adjacency (one-mode) or incidence (two-mode) matrices. Incidence matrices are typically inferred from unequal dimensions, but since in rare cases a matrix with equal dimensions may still be an incidence matrix, an additional argument twomode can be specified to override this heuristic.

This information is usually already embedded in {igraph}, {tidygraph}, and {network} objects.

See also

Other manipulations: add, grab, reformat, transform()

Examples

test <- data.frame(id1 = c("A","B","B","C","C"),
                   id2 = c("I","G","I","G","H"))
as_edgelist(test)
#>   from to
#> 1    A  I
#> 2    B  G
#> 3    B  I
#> 4    C  G
#> 5    C  H
as_matrix(test)
#>   G H I
#> A 0 0 1
#> B 1 0 1
#> C 1 1 0
as_igraph(test)
#> IGRAPH e49e236 DN-B 6 5 -- 
#> + attr: name (v/c), type (v/l)
#> + edges from e49e236 (vertex names):
#> [1] A->I B->G B->I C->G C->H
as_tidygraph(test)
#> # A tbl_graph: 6 nodes and 5 edges
#> #
#> # A rooted tree
#> #
#> # Node Data: 6 × 2 (active)
#>   name  type 
#>   <chr> <lgl>
#> 1 A     FALSE
#> 2 B     FALSE
#> 3 C     FALSE
#> 4 I     TRUE 
#> 5 G     TRUE 
#> 6 H     TRUE 
#> #
#> # Edge Data: 5 × 2
#>    from    to
#>   <int> <int>
#> 1     1     4
#> 2     2     5
#> 3     2     4
#> # … with 2 more rows
as_network(test)
#>  Network attributes:
#>   vertices = 6 
#>   directed = TRUE 
#>   hyper = FALSE 
#>   loops = FALSE 
#>   multiple = FALSE 
#>   bipartite = FALSE 
#>   total edges= 5 
#>     missing edges= 0 
#>     non-missing edges= 5 
#> 
#>  Vertex attribute names: 
#>     vertex.names 
#> 
#> No edge attributes