Learn R Programming

codep (version 1.2-4)

Euclid: Calculation of the Euclidean Distance

Description

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

Usage

Euclid(x, y, squared = FALSE)

Value

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.

Arguments

x

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

y

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

squared

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

Author

tools:::Rd_package_author("codep") Maintainer: tools:::Rd_package_maintainer("codep")

Details

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.

See Also

The `dist-class` and associated methods.

Examples

Run this code
## 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)
d1

## 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)

Run the code above in your browser using DataLab