Improved methods for handling ‘dist’ objects in R.

CRAN_Status_Badge R-CMD-check Build status Coverage Status DOI


Working with ‘dist’ objects in R can be challenging. What makes it challenging is that retrieving the distance between any two points requires specifying a 1 dimensional index. This is non-intuitive and requires the user to write additional code, which slows development and analysis. To avoid this problem, the function as.matrix has typically been used to convert a ‘dist’ object into a matrix. Once converted, distances can be accessed in the same way any element of a matrix is accessed, namely using two indices. Unfortunately, as.matrix conversion more than doubles memory usage. For small ‘dist’ objects this may not be a problem. However, for large ‘dist’ objects this can be quite problematic. The function get_dists in disttools solves this problem by facilitating rapid retrieval of the distance between any pair of points stored in a ‘dist’ object.


disttools can be installed from CRAN or GitHub by executing either of the following:

# Install from CRAN.
# Install from GitHub.


The package can be loaded by executing:

# Load the package.

The help file for the function get_dists can be accessed by executing:


If installed, the package vignette, complete with examples, can be viewed by executing:


Alternatively, the vignette is available on the package’s CRAN page: https://CRAN.R-project.org/package=disttools


The views expressed are those of the author(s) and do not reflect the official policy of the Department of the Army, the Department of Defense or the U.S. Government.