Suddenly, an error message, "Number of clusters 'k' must be in {1,2, .., n-1}; hence n >= 2", is displayed.

yes; we don't have your data; mtcars is an example ...

you have some code that before fviz_nblust, manually runs pam through possibilities; but you dont capture that; or look at it for anything....

You might consider changing it so that it shows you that highest possible k; you could then use that in fviz_nbclust

library(cluster)
y <- mtcars[1:8,]
for(i in 2:(nrow(y)-1)){
  pamx <- pam(y, k = i, metric = "euclidean")
  cat("k = ", i,"\n")
  print(unique(pamx$clustering))
}

note here mtcars is an example; that all forum users can use.
you could have provided your own example; but you didnt...
If you want to read advice on providing custom examples; I would encourage you to look here :

1 Like