[R] Cluster analysis: hclust manipulation possible?

Jopi Harri jopi.harri at utu.fi
Tue Nov 17 16:42:31 CET 2009

-------- Original Message --------
Subject: Re: [R] Cluster analysis: hclust manipulation possible?
Date: Mon, 16 Nov 2009 19:22:54 -0800
From: Charles C. Berry <cberry at tajo.ucsd.edu>
To: Jopi Harri <jopi.harri at utu.fi>
References: <4B016237.7050706 at utu.fi>
<Pine.LNX.4.64.0911160906420.27075 at tajo.ucsd.edu>
<4B01BC5D.3020504 at utu.fi>

On Mon, 16 Nov 2009, Jopi Harri wrote:

> On 16.11.2009 19:13, Charles C. Berry wrote:
>>> The question: Can this be accomplished in the *dendrogram plot*
>>> by manipulating the resulting hclust data structure or by some
>>> other means, and if yes, how?
>> Yes, you need to study
>>  	?hclust
>> particularly the part about 'Value' from which you will see what needs
>> modification.
>> Here is a very simple example:
>>> res <- hclust(dist(1-diag(3)*rnorm(3)))
>>> plot(res)
>>> res2 <- res
>>> res2$merge <- rbind(-cbind(1:3,4:6), matrix(ifelse( res2$merge<0, -res2$merge, res2$merge+sum(res2$merge<0)),2))
>>> res2$height <- c(rep(0,3), res2$height)
>>> res2$order <- as.vector( rbind(res2$order,(4:6)[res2$order]) )
>>> plot(res2)
>>> str( res )
>>> str( res2 )
> Dear Chuck,
> Many thanks for spending your valuable time in the suggestions
> and the example. However, the drawback is that as a humanist I
> have been having considerable difficulties in figuring out what
> exactly to do. After hours of experimenting I could modify
> another dendrogram (without crashing R), but still fail to get
> the result I want to: the added leaf is not attached to where I
> am intending to but instead, another adjacent leaves have their
> height turned to 0.
> The question, to put it more clearly perhaps: Is there any
> straightforward procedure to just add a single leaf to any
> dendrogram, next to an existing leaf at the height 0, and if
> there is, what might that be?
> As of now, it seems that the $merge has to be modified correctly,
> but what is the exact strategy, if there is one (other than
> redoing the whole clustering by hand)?

First, read the ?hclust page and see what it says about merge.

Then look at a really simple example like

cl <- hclust( dist( c(1,2,4) ) )


unclass( cl )

The unclass() strips the class attribute and allows print() to
give you a
bit more detail.

Now make the figure a bit more complicated:

cl2 <- hclust(dist(as.matrix(c(1,2,4,4.5))))

and see what has changed in $merge, $height, and $order.

Once you get the hang of it, you'll be in a position to modify an
hclust object.


p.s. it is best to post replies like yours to the whole list;
others may
want to know the same thing that you want to know or others may
give a
better reply than I have.

>> Alternatively, you could use as.dendrogram( res ) as the point of
>> departure and manipulate the value.
> Possibly, yes, but I am even less well-equipped with editing that
> sort of a data type.
> Sincerely,
> Jopi Harri
> Musicologist
> University of Turku
> Finland

Charles C. Berry                            (858) 534-2098
                                             Dept of
Family/Preventive Medicine
E mailto:cberry at tajo.ucsd.edu	            UC San Diego
http://famprevmed.ucsd.edu/faculty/cberry/  La Jolla, San Diego

More information about the R-help mailing list