[R] hierarchical clustering within a size limit

Peter Langfelder peter.langfelder at gmail.com
Wed May 11 19:53:48 CEST 2011


On Wed, May 11, 2011 at 10:12 AM, rna seq <rna.seeker at gmail.com> wrote:
> Hello List,
>
> I am trying to implement a hierarchical cluster using the hclust method
> agglomerative single linkage method with a small wrinkle. I would like to
> cluster a set of numbers on a number line only if they are within a distance
> of 500. I would then like to print out the members of this list.
>
> So far I can put a vector:
>> x<-c(2,10,200,300,600,700)
> into a distance matrix:
>> dist(x,method="manhattan")
>    1   2   3   4   5
> 2   8
> 3 198 190
> 4 298 290 100
> 5 598 590 400 300
> 6 698 690 500 400 100
>
> I can then cluster these distances using:
>>hc<-hclust(v, method = "complete")
>
> Next, I "believe" I set my distance limit in the cluster using the command
>
>>cutree(hc, h=500)
> 1 1 1 1 2 2 1 3
> [1] 1 1 1 1 2 2
>
> This seems to produce the correct result however, whatt I am unable to do is
> go back and extract and print out the members of each cluster. Any herp
> would be greatly appreciated.

Very simple.

labels = cutree(hc, h=500)
# members of cluster 1:
x[labels==1]
# members of cluster 2:
x[labels==2]

HTH,

Peter



More information about the R-help mailing list