Kwnwps
Kwnwps

Reputation: 363

dist() function in R outputs

I am curious about dist() output results that I get. More specifically, if I try

a=matrix(rnorm(1:100),nrow=10)
dist(a)

ok, I get what I expected

          1        2        3        4        5        6        7        8        9
2  3.700582                                                                        
3  3.793826 4.391523                                                               
4  3.063703 5.386797 5.494179                                                      
5  3.205545 4.464493 3.839944 3.763671                                             
6  3.868796 4.954696 3.340530 5.165389 3.589912                                    
7  3.906698 3.971069 3.405455 5.403859 4.284414 4.774963                           
8  2.620238 4.479064 5.403749 3.128820 4.237437 5.272889 5.551908                  
9  3.645784 4.586749 5.508289 3.333739 4.318391 6.113694 4.796519 3.641355         
10 2.292591 4.152536 4.869231 3.445773 3.557222 3.992109 4.061925 3.740496 4.225799

Nice, but: when I have a larger matrix

dim(Large_m)
[1] 978 235

I try

a=dist(Large_m)

I do not get a matrix, rather a 'Large dist' object as Rstudio says.

Is it correct if I use it with as.matrix ?

b=as.matrix(a)

b as I checked is indeed a matrix and seems like the distance matrix.

Also, I really need the row names in the distance matrix, not just numbers but in this way (working with as.matrix) I can't get it.

Seems I'm missing something, can't be so complicated to just create a distance matrix in R.

Upvotes: 4

Views: 7732

Answers (1)

WhiteViking
WhiteViking

Reputation: 3201

Here's an explicit example of what was suggested in the comments by @ColonelBeauvel. (I think there was some confusion due to a typo in a comment: as.matrix() vs is.matrix()...)

Large random matrix:

cols <- 325
rows <- 978

m <- matrix(rnorm(1:(rows*cols)), ncol=cols)

Let's give rows and columns a name:

rownames(m) <- paste0("r", 1:rows)
colnames(m) <- paste0("c", 1:cols)

Calculate distance object with dist then turn it into a regular matrix with as.matrix (as suggested by Colonel Beauvel):

d <- dist(m)
dm <- as.matrix(d)

Check the types:

class(d)
[1] "dist"
class(dm)
[1] "matrix"

Names are preserved in the distance matrix dm. Here's a small piece of it:

dm[1:3,1:3]
         r1       r2       r3
r1  0.00000 24.64059 26.63301
r2 24.64059  0.00000 25.69792
r3 26.63301 25.69792  0.00000

Hope this helps.

Upvotes: 4

Related Questions