% Generated by roxygen2: do not edit by hand
% Please edit documentation in R/class-network.R
\name{set_groups}
\alias{set_groups}
\title{Set Node Groups}
\usage{
set_groups(
  x,
  groups = NULL,
  type = c("group", "cluster", "layer"),
  nodes = NULL,
  layers = NULL,
  clusters = NULL
)
}
\arguments{
\item{x}{A cograph_network object.}

\item{groups}{Node groupings in one of these formats:
\itemize{
\item Character string: Community detection method ("louvain", "walktrap",
"fast_greedy", "label_prop", "infomap", "leiden")
\item Named list: Group name -> node vector mapping
(e.g., \code{list(A = c("N1","N2"), B = c("N3","N4"))})
\item Unnamed vector: Group assignment per node (same order as nodes)
\item Data frame: Must have "node"/"nodes" column plus one of
"layer"/"layers", "cluster"/"clusters", or "group"/"groups"
(plural forms are automatically normalized to singular)
\item NULL: Use \code{nodes} + one of \code{layers}/\code{clusters}/\code{groups} vectors
}}

\item{type}{Group type. One of \code{"group"} (default), \code{"cluster"},
or \code{"layer"}. Ignored when using vector arguments (\code{layers},
\code{clusters}, \code{groups}) since the type is inferred from which
argument is provided.}

\item{nodes}{Character vector of node labels. Use with \code{layers}, \code{clusters},
or \code{groups} to specify groupings via vectors instead of a data frame.}

\item{layers}{Character/factor vector of layer assignments (same length as \code{nodes}).}

\item{clusters}{Character/factor vector of cluster assignments (same length as \code{nodes}).}
}
\value{
The modified cograph_network object with \code{node_groups} set.
}
\description{
Assigns node groupings to a cograph_network object. Groups are stored as
metadata with a type column ("layer", "cluster", or "group") for use by
specialized plot functions.
}
\examples{
# Create network (symmetric for community detection)
mat <- matrix(runif(100), 10, 10)
mat <- (mat + t(mat)) / 2  # Make symmetric (undirected)
diag(mat) <- 0
rownames(mat) <- colnames(mat) <- paste0("N", 1:10)
net <- as_cograph(mat)

# Using vectors (recommended)
net <- set_groups(net,
  nodes = paste0("N", 1:10),
  layers = c(rep("Macro", 3), rep("Meso", 4), rep("Micro", 3))
)

# Named list -> layers
net <- set_groups(net, list(
  Macro = paste0("N", 1:3),
  Meso = paste0("N", 4:7),
  Micro = paste0("N", 8:10)
), type = "layer")

# Vector -> clusters
net <- set_groups(net, c("A", "A", "A", "B", "B", "B", "C", "C", "C", "C"),
                  type = "cluster")

# Community detection -> groups
net <- set_groups(net, "louvain", type = "group")

# Data frame with explicit columns
df <- data.frame(nodes = paste0("N", 1:10),
                 layers = rep(c("Top", "Bottom"), each = 5))
net <- set_groups(net, df)

# Check groups
get_groups(net)
}
\seealso{
\code{\link{get_groups}}, \code{\link{splot}}, \code{\link{detect_communities}}
}
