[R] Cluster analysis: hclust manipulation possible?

Jopi Harri jopi.harri at utu.fi
Tue Nov 17 17:06:46 CET 2009


On 17.11.2009 5:22, Charles C. Berry wrote:
> 
> Once you get the hang of it, you'll be in a position to modify an existing 
> hclust object.

I believe that I managed to solve the problem. (The code may not
be too refined, and my R is perhaps a bit dialectal. The function
may fail especially if the addition of multiple identical labels
is attempted.)

So, for the addition of a single duplicate label, one needs to
increment the positive values in $merge by one, and keep the
negative values except for the original of the duplicate which
will be given +1. Then, the duplicate pair [the value for the of
the new label being -(abs(min($merge))+1)] is added on top of $merge.

The other manipulations involved are the addition of height 0,
the label for the duplicate, and placing it properly in $order.

Once more thanks for the assistance.


Jopi Harri


dup.hclust=function(Hc,Label,DupLabel)
# We add to hclust Hc the duplicate DupLabel of Label.
# May fail in certain conditions, but shouldn't in normal use.
{
if (is.null(Hc$labels)) return("Labels are required!");
Mer=Hc$merge;
Hght=Hc$height;
Ord=Hc$order;
Labs=Hc$labels;
DupLNo=abs(min(Mer))+1;
LNo=which(Labs==Label);
LPlace=which(Labs[Ord]==Label);
Hght=c(0,Hght);
Labs=c(Labs,DupLabel);
Ord=c(Ord[1:LPlace[1]],DupLNo,Ord[LPlace[1]+1:(length(Ord))-LPlace[1]]);
NewMer=matrix(ifelse(Mer<0,Mer,Mer+1),nrow(Mer));
NewMer[NewMer==-LNo]=1;
NewMer=as.matrix(rbind(-cbind(LNo,DupLNo),NewMer));
NewMer=cbind(NewMer[,1],NewMer[,2]);
Hc$merge=NewMer;
Hc$height=Hght;
Hc$order=Ord;
Hc$labels=Labs;
return(Hc);
}




More information about the R-help mailing list