3.1 C
London
Friday, December 20, 2024
HomeRDescriptive Statistics in RHow to Use the dist Function in R (With Examples)

How to Use the dist Function in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

The dist() function in R can be used to calculate a distance matrix, which displays the distances between the rows of a matrix or data frame.

This function uses the following basic syntax:

dist(x, method=”euclidean”)

where:

  • x: The name of the matrix or data frame.
  • method: The distance measure to use. Default is “euclidean” but options include “maximum”, “manhattan”, “canberra”, “binary” or “minkowski”.

The following examples show how to use this function in practice with the following data frame:

#define four vectors
a #row bind four vectors into matrix
mat #view matrix
mat

  [,1] [,2] [,3] [,4]
a    2    4    4    6
b    5    5    7    8
c    9    9    9    8
d    1    2    3    3

Example 1: Use dist() to Calculate Euclidean Distance

The Euclidean distance between two vectors, A and B, is calculated as:

Euclidean distance = √Σ(Ai-Bi)2

The following code shows how to compute a distance matrix that shows the Euclidean distance between each row of a matrix in R:

#calculate Euclidean distance between each row in matrix
dist(mat)

          a         b         c
b  4.795832                    
c 10.148892  6.000000          
d  3.872983  8.124038 13.190906

Here’s how to interpret the output:

  • The Euclidean distance between row a and row b is 4.795832.
  • The Euclidean distance between row a and row c is 10.148892.
  • The Euclidean distance between row a and row d is 3.872983.
  • The Euclidean distance between row b and row c is 6.000000.
  • The Euclidean distance between row b and row d is 8.124038.
  • The Euclidean distance between row c and row d is 13.190906.

Example 2: Use dist() to Calculate Maximum Distance

The Maximum distance between two vectors, A and B, is calculated as the maximum difference between any pairwise elements.

The following code shows how to compute a distance matrix that shows the Maximum distance between each row of a matrix in R:

#calculate Maximum distance between each row in matrix
dist(mat, method="maximum")

  a b c
b 3    
c 7 4  
d 3 5 8

Example 3: Use dist() to Calculate Canberra Distance

The Canberra distance between two vectors, A and B, is calculated as:

Canberra distance = Σ |Ai-Bi| / |Ai| + |Bi|

The following code shows how to compute a distance matrix that shows the Canberra distance between each row of a matrix in R:

#calculate Canberra distance between each row in matrix
dist(mat, method="canberra")

          a         b         c
b 0.9552670                    
c 1.5484515 0.6964286          
d 1.1428571 1.9497835 2.3909091

Example 4: Use dist() to Calculate Binary Distance

The Binary distance between two vectors, A and B, is calculated as the proportion of elements that the two vectors share.

The following code shows how to compute a distance matrix that shows the Binary distance between each row of a matrix in R:

#calculate Binary distance between each row in matrix
dist(mat, method="binary")

  a b c
b 0    
c 0 0  
d 0 0 0

Example 5: Use dist() to Calculate Minkowski Distance

The Minkowski distance between two vectors, A and B, is calculated as:

Minkowski distance = (Σ|ai – bi|p)1/p

where i is the ith element in each vector and p is an integer.

The following code shows how to compute a distance matrix that shows the Minkowski distance (using p=3) between each row of a matrix in R:

#calculate Minkowski distance between each row in matrix
dist(mat, method="minkowski", p=3)

          a         b         c
b  3.979057                    
c  8.439010  5.142563          
d  3.332222  6.542133 10.614765

Additional Resources

How to Calculate Jaccard Similarity in R
How to Calculate Cosine Similarity in R
How to Calculate the Dot Product in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories