 # The Dist() Function In R: How to use dist() in R The dist() function in R is used to find the distance between the rows of a matrix. If you are not familiar with this function, then please check out our instructions below, we will discuss its syntax, application, and how to use it in your R program.

## Dist() function in R

### What does the dist() function do in R?

“Dist” stands for distance. So basically, the dist() function will calculate the distance of the rows of a data matrix. It can measure six different types of distances: Euclidean, Maximum, Canberra, Manhattan, Binary, and Minkowski.

Syntax:

dist(matrix, method)

Parameters:

• matrix: the matrix/data frame.
• method: the types of distance you want to calculate. It is one of these six: Euclidean, Maximum, Canberra, Manhattan, Binary, and Minkowski.

### How to use dist() in R?

There are six distance modes that the dist() function offers.

Euclidean distance: The distance between two points or two vectors. Calculated by this formula:

Let’s say we have this matrix:

vector1 <- c(12, 34, 13, 49, 67, 25, 63)
vector2 <- c(68, 91, 43, 16, 27, 32, 11)
vector3 <- c(18, 21, 75, 24, 92, 13, 62)

data <- rbind(vector1, vector2, vector3)
data
        [,1] [,2] [,3] [,4] [,5] [,6] [,7]
vector1   12   34   13   49   67   25   63
vector2   68   91   43   16   27   32   11
vector3   18   21   75   24   92   13   62

Now to calculate the Euclidean distance between the vectors in this matrix, do as follow:

dist(data, method = "euclidean")

Output:

          vector1   vector2
vector2 112.81401
vector3  73.78347 125.19984

As you can see, the results are well-demonstrated and easy to follow. For example, the Euclidean distance between vector_1 and vector_2 is 112.81401

If we have 4 vectors like this:

vector1 <- c(12, 34, 13, 49, 67, 25, 63)
vector2 <- c(68, 91, 43, 16, 27, 32, 11)
vector3 <- c(18, 21, 75, 24, 92, 13, 62)
vector4 <- c(20, 32, 19, 51, 60, 42, 38)

data <- rbind(vector1, vector2, vector3, vector4)
dist(data, method = "euclidean")

We would get:

          vector1  vector2  vector3
vector2 112.81401
vector3  73.78347 125.19984
vector4  32.72614  97.48846  80.19352

The 5 other methods (Maximum, Canberra, Manhattan, Binary, and Minkowski) are used just the same way. Simply change the method to whatever mode you want to use. For example, if you want to calculate the Canberra distance:

vector1 <- c(12, 34, 13, 49, 67, 25, 63)
vector2 <- c(68, 91, 43, 16, 27, 32, 11)
vector3 <- c(18, 21, 75, 24, 92, 13, 62)

data <- rbind(vector1, vector2, vector3)
dist(data, method = "canberra")

Output:

         vector1 vector2
vector2 3.450448
vector3 1.964397 3.344653

You can search the internet for the definition of each type of distance or check out this link for more information.

## Summary

In this tutorial, we have shown you the dist() function in R and how to use it in your program. The dist() function helps us calculate six types of distances between vectors in a matrix.

Maybe you are interested:

Posted in R