Amri Muhaimin
Amri Muhaimin

Reputation: 15

Looping on Cluster, return as Vector

I have a for loop that something like this

for (n in 2 :10){somc[n] <- cutree(hclust(dist(som_model$codes[[1]])),n)}

And the result is error like this

In somc[n] <- cutree(hclust(dist(som_model$codes[[1]])), n) : number of items to replace is not a multiple of replacement length

Please help me to fix this. I want the result as a vector that describe 2 till 10 cluster.

Upvotes: 0

Views: 57

Answers (1)

Jonny Phelps
Jonny Phelps

Reputation: 2727

Can you supply some more information, please? Is somc a list? the right hand side of this equation won't produce a single number. I haven't used this before, but this is how I might approach it:

input <- matrix(runif(100), ncol = 2)
output_list <- list()
num_clusters <- 2:10
for(i in num_clusters){
  dist_matrix    <- dist(input)
  my_cluster     <- hclust(dist_matrix)
  cut_my_cluster <- cutree(my_cluster, i)
  output_list[[paste(i)]] <- cut_my_cluster
}
output_list

Is this applicable?

Best, Jonny

Upvotes: 1

Related Questions