Learn R Programming

migraph (version 0.9.3)

coercion: Coercion between migraph-compatible object classes

Description

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

  • edgelists, as data frames or tibbles

  • adjacency and incidence 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.

Usage

as_edgelist(object)

as_matrix(object)

as_igraph(object, twomode = FALSE)

as_tidygraph(object, twomode = FALSE)

as_network(object)

Arguments

object

An object of a migraph-consistent class:

  • matrix, 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 the heuristics for distinguishing incidence from adjacency matrices. By default FALSE.

Value

The currently implemented coercions or translations are:

to/from edgelists matrices igraph tidygraph network
edgelists (data frames) X X X X X
matrices X X X X X
igraph X X X X X
tidygraph X X X X X
network X X X X X

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 manipulation: add, is(), to

Examples

Run this code
# NOT RUN {
test <- data.frame(id1 = c("A","B","B","C","C"),
                   id2 = c("I","G","I","G","H"))
as_edgelist(test)
as_matrix(test)
as_igraph(test)
as_tidygraph(test)
as_network(test)
# }

Run the code above in your browser using DataLab