How do I manipulate/access elements of an instance of “dist” class using core R?

前端 未结 12 1944
傲寒
傲寒 2021-02-02 10:50

A basic/common class in R is called \"dist\", and is a relatively efficient representation of a symmetric distance matrix. Unlike a \"matrix\" object,

12条回答
  •  孤城傲影
    2021-02-02 11:15

    I don't have a straight answer to your question, but if you are using the Euclidian distance, have a look at the rdist function from the fields package. Its implementation (in Fortran) is faster than dist, and the output is of class matrix. At the very least, it shows that some developers have chosen to move away from this dist class, maybe for the exact reason you are mentioning. If you are concerned that using a full matrix for storing a symmetric matrix is an inefficient use of memory, you could convert it to a triangular matrix.

    library("fields")
    points <- matrix(runif(1000*100), nrow=1000, ncol=100)
    
    system.time(dist1 <- dist(points))
    #    user  system elapsed 
    #   7.277   0.000   7.338 
    
    system.time(dist2 <- rdist(points))
    #   user  system elapsed 
    #  2.756   0.060   2.851 
    
    class(dist2)
    # [1] "matrix"
    dim(dist2)
    # [1] 1000 1000
    dist2[1:3, 1:3]
    #              [,1]         [,2]         [,3]
    # [1,] 0.0000000001 3.9529674733 3.8051198575
    # [2,] 3.9529674733 0.0000000001 3.6552146293
    # [3,] 3.8051198575 3.6552146293 0.0000000001
    

提交回复
热议问题