Skip to content

Commit 0264374

Browse files
kei51eclaude
andcommitted
fix(kmeans): return NA for silhouette summary aggregates on degenerate input
When per-row silhouette is all-NA (fewer than 2 clusters or distinct points), the per-cluster summary aggregates previously leaked NaN (avg/pct) and Inf (min). Guard the all-NA case so avg/min/pct each return NA consistently. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 8ab44d2 commit 0264374

1 file changed

Lines changed: 4 additions & 3 deletions

File tree

R/prcomp.R

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -181,9 +181,10 @@ tidy.prcomp_exploratory <- function(x, type="variances", n_sample=NULL, pretty.n
181181
dplyr::mutate(.cluster_key = as.character(x$kmeans$cluster)) %>%
182182
dplyr::group_by(.cluster_key) %>%
183183
dplyr::summarise(
184-
avg_silhouette = mean(silhouette_score, na.rm = TRUE),
185-
min_silhouette = suppressWarnings(min(silhouette_score, na.rm = TRUE)),
186-
pct_negative = mean(silhouette_score < 0, na.rm = TRUE),
184+
# Guard the degenerate all-NA case so it yields NA (not NaN/Inf) consistently.
185+
avg_silhouette = if (all(is.na(silhouette_score))) NA_real_ else mean(silhouette_score, na.rm = TRUE),
186+
min_silhouette = if (all(is.na(silhouette_score))) NA_real_ else min(silhouette_score, na.rm = TRUE),
187+
pct_negative = if (all(is.na(silhouette_score))) NA_real_ else mean(silhouette_score < 0, na.rm = TRUE),
187188
.groups = "drop"
188189
)
189190
res <- res %>%

0 commit comments

Comments
 (0)