In this article, we will show you what the dist() function does in R, and how to use the dist
function in R. You can use the dist() function to calculate the distance of the matrix. Let’s learn more about it with the explanation and examples below.
Dist Function In R
What does the dist function do in R?
The dist()
function in R is used to calculate the distance of the matrix by the specified method. It is widely used in mathematics when working with the matrix. You can use it with one of the methods: euclidean, manhattan, maximum, binary, minkowski, or canberra. Let’s take a look at the syntax of the dist() function.
Syntax
dist(data, method = "euclidean", diag, upper , p = 2)
Parameters
- data: The numeric data frame or matrix.
- method: One of the methods: euclidean, manhattan, maximum, binary, minkowski or canberra.
- diag: The default is FALSE. Print the diagonal of the distance matrix of not.
- upper: The default is FALSE. Print the upper triangle of the distance matrix or not.
- p: The default is 2. The power of the ‘minkowski’ distance. Only use it when the method is ‘minkowski’.
After learning the usage and the syntax of the dist() function in R, you will learn how to use this function in the next title below.
How To Use The Dist Function In R
You can use the dist() function to calculate the distance matrix with the specified method.
Calculate the distance of the matrix with the default syntax
You can calculate the euclidean
distance with the default syntax.
Look at the example below.
# Create the matrix. mat <- matrix (1:20, nrow = 4, ncol = 5) # Calculate the euclidean distance of this matrix. dist(mat)
Output
1 2 3
2 2.236068
3 4.472136 2.236068
4 6.708204 4.472136 2.236068
Calculate the canberra distance of the matrix
You can calculate the 'canberra'
distance of the matrix by assigning the value to the ‘method’ parameter.
Look at the example below.
# Create the matrix. mat <- matrix (1:20, nrow = 4, ncol = 5) # Calculate 'canberra' the distance of this matrix. dist(mat, method = 'canberra')
Output
1 2 3
2 0.5424825
3 0.8936508 0.3860519
4 1.1581557 0.6863978 0.3109012
Calculate the distance of the matrix and print out the diagonal distance
You calculate the distance of the matrix and print out the diagonal distance by assigning a value to the 'diag'
parameter.
Look at the example below.
# Create the matrix. mat <- matrix (1:20, nrow = 4, ncol = 5) dist(mat, diag = TRUE)
Output
1 2 3 4
1 0.000000
2 2.236068 0.000000
3 4.472136 2.236068 0.000000
4 6.708204 4.472136 2.236068 0.000000
Calculate the distance of the matrix and print out the upper triangle of the distance matrix
You calculate the distance of the matrix and print out the upper triangle of the distance matrix by setting a value to the 'upper'
parameter as TRUE.
Look at the example below.
# Create the matrix. mat <- matrix (1:20, nrow = 4, ncol = 5) dist(mat, upper = TRUE)
Output
1 2 3 4
1 2.236068 4.472136 6.708204
2 2.236068 2.236068 4.472136
3 4.472136 2.236068 2.236068
4 6.708204 4.472136 2.236068
You can learn how to create the identity matrix in the article here.
Summary
You have learned about how to calculate the distance matrix by the dist
function in R. You can customize this function to fit your demand by adjusting its parameters. We hope this tutorial is helpful to you. Thanks!
Leave a Reply