Bobe Kryant
Bobe Kryant

Reputation: 2100

How to insert a distance matrix into R and run hierarchical clustering

I know If I have raw data, I can create a distance matrix, however for this problem I have a distance matrix and I want to be able to run commands in R on it, like hclust. Below is my distance matrix I want in R. I am not sure storing this data in matrix form will work as I will be unable to run hclust on a matrix.

enter image description here

I have tried to create it using as.dist functions to no avail. My faulty code:

test=as.dist(c(.76,2.97,4.88,3.86,.8,4.17,1.96,.21,1.51,.51), diag = FALSE, upper = FALSE)
test
      1    2    3    4    5    6    7    8    9
2  2.97                                        
3  4.88 2.97                                   
4  3.86 4.88 0.51                              
5  0.80 3.86 2.97 0.21                         
6  4.17 0.80 4.88 1.51 0.80                    
7  1.96 4.17 3.86 0.51 4.17 0.51               
8  0.21 1.96 0.80 2.97 1.96 2.97 0.80          
9  1.51 0.21 4.17 4.88 0.21 4.88 4.17 0.21     
10 0.51 1.51 1.96 3.86 1.51 3.86 1.96 1.51 0.51

Upvotes: 3

Views: 11090

Answers (1)

RHertel
RHertel

Reputation: 23788

Since you already have the distance values, you don't need to use dist() to calculate them. The data can be stored in a regular matrix

test <- matrix(ncol=5,nrow=5)
test[lower.tri(test)] <- c(.76,2.97,4.88,3.86,.8,4.17,1.96,.21,1.51,.51)
diag(test) <- 0

> test
     [,1] [,2] [,3] [,4] [,5]
[1,] 0.00   NA   NA   NA   NA
[2,] 0.76 0.00   NA   NA   NA
[3,] 2.97 0.80 0.00   NA   NA
[4,] 4.88 4.17 0.21 0.00   NA
[5,] 3.86 1.96 1.51 0.51    0

In order to apply hclust(), this matrix can then be converted to a distance matrix with as.dist():

> test <- as.dist(test, diag = TRUE)
     1    2    3    4    5
1 0.00                    
2 0.76 0.00               
3 2.97 0.80 0.00          
4 4.88 4.17 0.21 0.00     
5 3.86 1.96 1.51 0.51 0.00
> hclust(test)
#
#Call:
#hclust(d = test)
#
#Cluster method   : complete 
#Number of objects: 5 
> plot(hclust(test))

enter image description here

Upvotes: 11

Related Questions