Learn R Programming

cograph (version 2.0.0)

detect_communities: Detect Communities in a Network

Description

Detects communities (clusters) in a network using various community detection algorithms. Returns a data frame with node-community assignments.

Usage

detect_communities(x, method = "louvain", directed = NULL, weights = TRUE)

Value

A data frame with columns:

  • node: Node labels/names

  • community: Integer community membership

Arguments

x

Network input: matrix, igraph, network, cograph_network, or tna object.

method

Community detection algorithm to use. One of:

  • "louvain": Louvain method (default, fast and accurate)

  • "walktrap": Walktrap algorithm based on random walks

  • "fast_greedy": Fast greedy modularity optimization

  • "label_prop": Label propagation algorithm

  • "infomap": Infomap algorithm based on information flow

  • "leiden": Leiden algorithm (improved Louvain)

directed

Logical or NULL. If NULL (default), auto-detect from matrix symmetry. Set TRUE to force directed, FALSE to force undirected.

weights

Logical. Use edge weights for community detection. Default TRUE.

Examples

Run this code
# Basic usage
adj <- matrix(c(0, .5, .8, 0,
                .5, 0, .3, .6,
                .8, .3, 0, .4,
                 0, .6, .4, 0), 4, 4, byrow = TRUE)
rownames(adj) <- colnames(adj) <- c("A", "B", "C", "D")
detect_communities(adj)

# Different algorithm
detect_communities(adj, method = "walktrap")

Run the code above in your browser using DataLab