network: Network Objects

View source: R/constructors.R

networkR Documentation

Network Objects


Construct, coerce to, test for and print network objects.

Usage, ...)

  vertex.attr = NULL,
  vertex.attrnames = NULL,
  directed = TRUE,
  hyper = FALSE,
  loops = FALSE,
  multiple = FALSE,
  bipartite = FALSE,


## S3 method for class 'data.frame'
  directed = TRUE,
  vertices = NULL,
  hyper = FALSE,
  loops = FALSE,
  multiple = FALSE,
  bipartite = FALSE,
  bipartite_col = "is_actor",

## S3 method for class 'network'
  matrix.type = which.matrix.type(x),
  mixingmatrices = FALSE,
  na.omit = TRUE,
  print.adj = FALSE,

## S3 method for class 'network'
summary(object, na.omit = TRUE, mixingmatrices = FALSE, print.adj = TRUE, ...)



for network, a matrix giving the network structure in adjacency, incidence, or edgelist form; otherwise, an object of class network.


additional arguments.


optionally, a list containing vertex attributes.


optionally, a list containing vertex attribute names.


logical; should edges be interpreted as directed?


logical; are hyperedges allowed?


logical; should loops be allowed?


logical; are multiplex edges allowed?


count; should the network be interpreted as bipartite? If present (i.e., non-NULL, non-FALSE) it is the count of the number of actors in the bipartite network. In this case, the number of nodes is equal to the number of actors plus the number of events (with all actors preceeding all events). The edges are then interpreted as nondirected. Values of bipartite==0 are permited, indicating a bipartite network with zero-sized first partition.


If x is a data.frame, vertices is an optional data.frame containing the vertex attributes. The first column is assigned to the "vertex.names" and additional columns are used to set vertex attributes using their column names. If bipartite is TRUE, a logical column named "is_actor" (or the name of a column specified using the bipartite_col parameter) can be provided indicating which vertices should be considered as actors. If not provided, vertices referenced in the first column of x are assumed to be the network's actors. If your network has isolates (i.e. there are vertices referenced in vertices that are not referenced in x), the "is_actor" column is required.


character(1L), default: "is_actor". The name of the logical column indicating which vertices should be considered as actors in bipartite networks.


one of "adjacency", "edgelist", "incidence". See edgeset.constructors for details and optional additional arguments


logical; print the mixing matrices for the discrete attributes?


logical; omit summarization of missing attributes in network?


logical; print the network adjacency structure?


an object of class network.


network constructs a network class object from a matrix representation. If the matrix.type parameter is not specified, it will make a guess as to the intended edgeset.constructors function to call based on the format of these input matrices. If the class of x is not a matrix, network construction can be dispatched to other methods. For example, If the ergm package is loaded, network() can function as a shorthand for with x as an integer specifying the number of nodes to be created in the random graph.

If the ergm package is loaded, network can function as a shorthand for if x is an integer specifying the number of nodes. See the help page for in ergm package for details.

network.copy creates a new network object which duplicates its supplied argument. (Direct assignment with <- should be used rather than network.copy in most cases.) tries to coerce its argument to a network, using the functions if x is a matrix. (If the argument is already a network object, it is returned as-is and all other arguments are ignored.) tests whether its argument is a network (in the sense that it has class network). prints a network object in one of several possible formats. It also prints the list of global attributes of the network. provides similar information.


network,, and all return a network class object; returns TRUE or FALSE.


Between versions 0.5 and 1.2, direct assignment of a network object created a pointer to the original object, rather than a copy. As of version 1.2, direct assignment behaves in the same manner as network.copy. Direct use of the latter is thus superfluous in most situations, and is discouraged.

Many of the network package functions modify their network object arguments in-place. For example,,"myVal",5) will have the same effect as net<,"myVal",5). Unfortunately, the current implementation of in-place assignment breaks when the network argument is an element of a list or a named part of another object. So[[1]],"myVal",5) will silently fail to modify its network argument, likely leading to incorrect output.


Carter T. Butts and David Hunter


Butts, C. T. (2008). “network: a Package for Managing Relational Data in R.” Journal of Statistical Software, 24(2).

See Also

network.initialize, attribute.methods,,, deletion.methods, edgeset.constructors, network.indicators,


m <- matrix(rbinom(25,1,.4),5,5)
diag(m) <- 0
g <- network(m, directed=FALSE)

h <- network.copy(g)       #Note: same as h<-g

# networks from data frames ===========================================================
#* simple networks ====================================================================
simple_edge_df <- data.frame(
  from = c("b", "c", "c", "d", "a"),
  to = c("a", "b", "a", "a", "b"),
  weight = c(1, 1, 2, 2, 3),
  stringsAsFactors = FALSE

# simple networks with vertices =======================================================
simple_vertex_df <- data.frame(
  name = letters[1:5],
  residence = c("urban", "rural", "suburban", "suburban", "rural"),
  stringsAsFactors = FALSE
simple_vertex_df, vertices = simple_vertex_df),
  directed = FALSE, vertices = simple_vertex_df,
  multiple = TRUE

#* splitting multiplex data frames into multiple networks =============================
simple_edge_df$relationship <- c(rep("friends", 3), rep("colleagues", 2))

lapply(split(simple_edge_df, f = simple_edge_df$relationship),,
  vertices = simple_vertex_df

#* bipartite networks without isolates ================================================
bip_edge_df <- data.frame(
  actor = c("a", "a", "b", "b", "c", "d", "d", "e"),
  event = c("e1", "e2", "e1", "e3", "e3", "e2", "e3", "e1"),
  actor_enjoyed_event = rep(c(TRUE, FALSE), 4),
  stringsAsFactors = FALSE

bip_node_df <- data.frame(
  node_id = c("a", "e1", "b", "e2", "c", "e3", "d", "e"),
  node_type = c(
    "person", "event", "person", "event", "person",
    "event", "person", "person"
  color = c(
    "red", "blue", "red", "blue", "red", "blue",
    "red", "red"
  stringsAsFactors = FALSE
bip_node_df, directed = FALSE, bipartite = TRUE), directed = FALSE, vertices = bip_node_df, bipartite = TRUE)

#* bipartite networks with isolates ===================================================
bip_nodes_with_isolates <- rbind(
    node_id = c("f", "e4"),
    node_type = c("person", "event"),
    color = c("red", "blue"),
    stringsAsFactors = FALSE
# indicate which vertices are actors via a column named `"is_actor"`
bip_nodes_with_isolates$is_actor <- bip_nodes_with_isolates$node_type == "person"
  directed = FALSE, vertices = bip_nodes_with_isolates,
  bipartite = TRUE

#* hyper networks from data frames ====================================================
hyper_edge_df <- data.frame(
  from = c("a/b", "b/c", "c/d/e", "d/e"),
  to = c("c/d", "a/b/e/d", "a/b", "d/e"),
  time = 1:4,
  stringsAsFactors = FALSE

# split "from" and "to" at `"/"`, coercing them to list columns
hyper_edge_df$from <- strsplit(hyper_edge_df$from, split = "/")
hyper_edge_df$to <- strsplit(hyper_edge_df$to, split = "/")
  directed = FALSE, vertices = simple_vertex_df,
  hyper = TRUE, loops = TRUE

# convert network objects back to data frames =========================================
simple_g <-, vertices = simple_vertex_df), unit = "vertices")

bip_g <-,
  directed = FALSE, vertices = bip_node_df,
  bipartite = TRUE
), unit = "vertices")

hyper_g <-,
  directed = FALSE, vertices = simple_vertex_df,
  hyper = TRUE, loops = TRUE
), unit = "vertices")

network documentation built on Feb. 16, 2023, 6:11 p.m.