1
votes

Given a distance matrix

d = matrix(c(0,2.5,4.5,2.5,0,3.4,4.5,3.4,0), nrow=3),

how to do Hierarchical clustering using R? Using

hclust(d)

it is given me the error

Error in if (is.na(n) || n > 65536L) stop("size cannot be NA nor exceed 65536") :  missing value where TRUE/FALSE needed.
1

1 Answers

3
votes

You need to convert it to an object with dist,

d1 = as.dist(d)
hclust(d1)

If you examine d1

R> str(d1)
Class 'dist'  atomic [1:3] 2.5 4.5 3.4
  ..- attr(*, "Size")= int 3
  ..- attr(*, "call")= language as.dist.default(m = d)
  ..- attr(*, "Diag")= logi FALSE
  ..- attr(*, "Upper")= logi FALSE 

You can see that R is being clever with what it stores; it only requires the lower triangular matrix.