Convert objects to class "network"

Share:

Description

Convert objects to class "network"

Usage

1
2
3
4
5
6
7
8
  asNetwork(x, ...)

  ## S3 method for class 'data.frame'
 asNetwork(x, directed = TRUE,
    vertices = NULL, ...)

  ## S3 method for class 'igraph'
 asNetwork(x, attrmap = attrmap(), ...)

Arguments

x

an R object to be coerced, see Details for the description of available methods

attrmap

data.frame with attribute copy/rename rules, see attrmap

directed

logical, whether the created network should be directed

vertices

NULL or data frame, optional data frame containing vertex attributes

...

other arguments from/to other methods

Details

This is a generic function which dispatches on argument x. It creates objects of class "network" from other R objects.

The method for data frames is inspired by the similar function in package igraph: graph.data.frame. It assumes that first two columns of x constitute an edgelist. The remaining columns are interpreted as edge attributes. Optional argument vertices allows for including vertex attributes. The first column is assumed to vertex id, the same that is used in the edge list. The remaining colums are interpreted as vertex attributes.

The method for objects of class "igraph" takes the network of that class and converts it to data frames using asDF. The network is recreated in class "network" using asNetwork.data.frame. The function currently does not support bipartite "igraph" networks.

Value

Object of class "network".

See Also

graph.data.frame

asIgraph for conversion in the other direction.

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
# require package 'network' as 'asNetwork' generic is defined there
if(require(network, quietly=TRUE))
{
  ### demonstrating method for data frames
  l <- asDF(exNetwork)
  g <- asNetwork( l$edges, vertices=l$vertexes)
  stopifnot(all.equal(g, exNetwork))

  ### method for igraph objects
  ig <- asNetwork(exIgraph)
  identical( as.numeric(as.matrix(g, "adjacency")),
            as.numeric(as.matrix(ig, "adjacency")))
}