Learn R Programming

cograph (version 2.7.2)

contract_nodes: Contract Nodes into Groups

Description

Replaces each group of nodes with a single node whose edges aggregate the edges of its members. The counterpart of igraph::contract() and tidygraph's to_contracted(), and the network form of what summarize_clusters() computes inside an analysis object.

Usage

contract_nodes(
  x,
  groups,
  weight = c("sum", "mean", "max", "min"),
  loops = FALSE,
  keep_format = FALSE,
  directed = NULL
)

Value

A cograph_network with one node per group, labeled by group name, or the input format when keep_format = TRUE.

Arguments

x

Network input.

groups

Group assignment. Either a vector with one entry per node (in node order), or a named list mapping group name to node labels.

weight

How to aggregate the weights of the edges that fall between two groups: "sum" (default), "mean", "max" or "min".

loops

Logical. Keep the within-group edges as self-loops on the contracted node. Default FALSE.

keep_format

Logical. Return the input format when TRUE.

directed

Logical or NULL. If NULL (default), auto-detect.

See Also

summarize_clusters, detect_communities, split_components

Examples

Run this code
adj <- matrix(c(0, 1, 1, 0,
                1, 0, 0, 1,
                1, 0, 0, 1,
                0, 1, 1, 0), 4, 4, byrow = TRUE)
rownames(adj) <- colnames(adj) <- c("A", "B", "C", "D")

contract_nodes(adj, groups = c("left", "left", "right", "right"))

Run the code above in your browser using DataLab