build_solution_from_labeled_clusters: Combine nTARP BestCluster solutions into one table and assign...

View source: R/build_solution_from_labeledclusters.R

build_solution_from_labeled_clustersR Documentation

Combine nTARP BestCluster solutions into one table and assign stable final cluster IDs

Description

Consolidates a set of nTARP "best cluster" solutions (branching runs) that contain 'LabeledClusters' into a single ID-level data frame. For each run, the function adds a per-run cluster assignment column, constructs a concatenated 'ClusterPath', and assigns a stable numeric 'FinalClusterID' based on unique 'ClusterPath' values.

Usage

build_solution_from_labeled_clusters(
  nTARP_best_clusters,
  ids = NULL,
  contextual_variables_df = NULL
)

Arguments

nTARP_best_clusters

A list of run objects. Each run object must either:

  • contain 'LabeledClusters' at the top level, or

  • contain a top-level list element that contains 'LabeledClusters'.

'LabeledClusters' must be a named list with '"Cluster 1"' and '"Cluster 2"' entries containing IDs. The list should be named (recommended). Names are used as run identifiers (column suffixes). If unnamed, runs are labeled sequentially ('run1', 'run2', ...).

ids

A vector of IDs (character or coercible) that defines the universe of rows in the output.

contextual_variables_df

Optional 'data.frame' of contextual variables to merge in. If supplied, it must contain a column named 'id_name' (default '"mcid"'). The merge is a left join on the provided 'ids' vector, so every ID in 'ids' appears in the output.

Value

A 'data.frame' with one row per ID in 'ids', optionally merged with contextual variables, plus per-run cluster columns, 'ClusterPath', and 'FinalClusterID'.

Examples

data <- data.frame(X1 = c(0.5, -0.2, 0.1, 0.3, -0.1, 0.2, 5.2, 4.8, 5.1, 5.0,
                         -4.5, -5.2, -4.8, -5.1, -4.9, -5.3, 0.0, 0.2, 5.3, -5.0),
  X2 = c(0.3, -0.1, 0.2, 0.1, 0.0, 0.2, 5.0, 4.9, 5.3, 5.1,
         5.0, 5.2, 4.7, 4.9, 5.1, 4.8, -0.2, 0.0, 5.2, -4.9),
  X3 = c(0.4, 0.0, 0.1, -0.1, 0.2, 0.0, 5.1, 4.7, 5.2, 5.0,
         -5.0, -4.8, -5.3, -5.1, -4.9, -5.2, 0.1, 0.3, 5.0, -5.1)
)
nTARP_result <- nTARP_bisecting(data = data,number_of_projections = 100,withinss_threshold = 0.36)
result <- build_solution_from_labeled_clusters(nTARP_best_clusters = nTARP_result$BestClusters,
ids = 1:10, contextual_variables_df = data)
str(result)

nTARP documentation built on March 20, 2026, 5:09 p.m.