Moritz Lennert
2006-May-08 09:34 UTC
[R] finding centroids of clusters created with hclust
Hello, Can someone point me to documentation or ideas on how to calculate the centroids of clusters identified with hclust ? I would like to be able to chose the number of clusters (in the style of cutree) and then get the centroids of these clusters. This seems like a quite obvious task to me, but I haven't been able to put my hands on a relevant command. Thank you, Moritz
Moritz Lennert
2006-May-10 16:59 UTC
[R] finding centroids of clusters created with hclust
Replying to myself for the record: Moritz Lennert wrote:> Hello, > > Can someone point me to documentation or ideas on how to calculate the > centroids of clusters identified with hclust ? > > I would like to be able to chose the number of clusters (in the style of > cutree) and then get the centroids of these clusters. > > This seems like a quite obvious task to me, but I haven't been able to > put my hands on a relevant command.Here's a simple function that does the job for me: Variables: data: matrix of original (absolute value) data introduced into hclust or HierClust clust: result of a 'cutree' call on the results of the hclust or HierClust call Value: a matrix of relative values of the variables at the centroids of the types function (data, clust) { nvars=length(data[1,]) ntypes=max(clust) centroids<-matrix(0,ncol=nvars,nrow=ntypes) for(i in 1:ntypes) { c<-rep(0,nvars) n<-0 for(j in names(clust[clust==i])) { n<-n+1 c<-c+data[j,] } centroids[i,]<-c/n } rownames(centroids)<-c(1:ntypes) colnames(centroids)<-colnames(data) centroids } Moritz