简体   繁体   中英

Distance matrix to dist element

New to R here. I have computed a distance matrix over, say, points a, b, c:

(a,a) = 0;
(a,b) = 1;
(a,c) = 5;
(b,a) = 1;
(b,b) = 0;
(b,c) = 7;
(c,a) = 5;
(c,b) = 7;
(c,c) = 0;

And I want to use a function that expects the 'dist' R element. My distance function is the result of a computation on the items' coordinates, but it's not a standard computation, so I can't create the dist element using the dist constructor, which expects only several predefined distance measures.

Bottom line is that I want to create the dist element out of the matrix that I have computed.

Appreciate your help!

You can create a distance class object from a matrix

Just create the matrix and use as.dist

eg

 as.dist(matrix( c(0, 1, 5, 1, 0, 7, 5, 7, 0), ncol=3))


  1 2
2 1  
3 5 7

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM