How to convert a symmetric matrix into "dist" object? How to convert a symmetric matrix into "dist" object? r r

How to convert a symmetric matrix into "dist" object?


It sounds like you already have a matrix calculated, and want to use that in hclust. Like @shadow said, you can use as.dist(yourMatrix) to convert to the dist format.

Given a symmetric table of distances:

> yourMatrix<-matrix(c(1,2,3,4,2,1,2,1,3,2,1,3,4,1,3,1), nrow=4)     [,1] [,2] [,3] [,4][1,]    1    2    3    4[2,]    2    1    2    1[3,]    3    2    1    3[4,]    4    1    3    1>>as.dist(yourMatrix)  1 2 32 2    3 3 2  4 4 1 3

Make sure that the values in your matrix are dissimilarity, or distance metrics rather than similarity scores.


Is this what you need? dist(matrix(1:16, nrow=4))