Euclid: Calculation of the Euclidean Distance

View source: R/Euclid.R

EuclidR Documentation

Calculation of the Euclidean Distance


Function Euclid carries out the calculation of pairwise Euclidean distances within a set of coordinates or between two sets thereof, with optional weights.


Euclid(x, y, squared = FALSE)



A set of coordinates in the form of a matrix or data.frame.


An optional second set of coordinates in the same dimensions as argument x.


Should the squared Euclidean distances be returned (default: FALSE).


When only one set of coordinates is given to the function (i.e., when argument y is omitted), the function returns the pairwise distances in the form of a 'dist-class' object representing a lower-triangle matrix. If weights are omitted, the result is identical to that produced by function dist with argument method = "euclidean" (the function's default).

The standard 'R' function used to calculate the Euclidean distance (dist), only allows one to calculate pairwise distances between the rows of a single matrix of Cartesian coordinates and return a 'dist-class' object, which is a one-dimensional array meant to be interpreted as a lower-triangular matrix. Function Euclid can also be provided two data matrices (arguments x and y) and output a rectangular matrix of the Euclidean distances.


A 'dist-class' object or, whenever y is provided, a matrix with as many rows as the number of rows in x and as many columns as the number of rows in y.


Guillaume Guenard and Pierre Legendre, Bertrand Pages Maintainer: Guillaume Guenard <>

See Also

The 'dist-class' and associated methods.


## A set of reference points:
x <- cbind(c(1,4,5,2,8,4), c(3,6,7,1,3,2))
dimnames(x) <- list(LETTERS[1:6], c("x", "y"))

## The pairwise Euclidean distances among the reference points: 
d1 <- Euclid(x)

## That result is the same as that obtained from function dist:
d2 <- dist(x, method = "euclidean")
all(d1 == d2)

## A second set of points:
y <- cbind(c(3,5,7), c(3,6,8))
dimnames(y) <- list(LETTERS[7:9], c("x", "y"))

## The distances between the points in y (rows) and x (columns):
Euclid(x, y)

guenardg/codep documentation built on April 15, 2023, 6:47 a.m.