📜  如何在 R 中使用 Dist函数?

📅  最后修改于: 2022-05-13 01:54:24.680000             🧑  作者: Mango

如何在 R 中使用 Dist函数?

在本文中,我们将了解如何在 R 编程语言中使用 dist()函数。

R 提供了一个内置的 dist()函数,使用它我们可以计算二维向量中每对唯一向量之间的六种不同的距离。 dist() 方法接受一个数值矩阵作为参数和一个表示要测量的距离类型的方法。该方法必须是这些距离之一 - 欧几里得、最大值、曼哈顿、堪培拉、二进制和 Minkowski。它也接受其他参数,但它们是可选的。

现在让我们看看如何使用 dist()函数计算这些距离。

欧几里得距离

欧几里得空间中两点之间的欧几里得距离基本上是两点之间的线段的长度。它可以借助毕达哥拉斯定理从点的笛卡尔坐标中计算出来,因此有时称为毕达哥拉斯距离。

例如,在具有两个点 Point1 (x 1 ,y 1 ) 和 Point2 (x 2 ,y 2 ) 的二维空间中,欧几里得距离由 √(x 1 – x 2 ) 2 + (y 1 –是2 ) 2

两个向量之间的欧几里得距离由下式给出,

√Σ(vect1i - vect2i)2

在哪里,

  • vect1 是第一个向量
  • vect2 是第二个向量

例如,给定两个向量,vect1 为 (2, 1, 5, 8),vect2 为 (1, 2, 4, 9)。它们的欧几里得距离由 √(2 – 1) 2 + (1 – 2) 2 + (5 – 4) 2 + (8 – 9) 2给出,等于 2。

句法:

示例:欧几里得距离

R
# R program to illustrate how to calculate
# euclidean distance using dist() function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3,
                            vect4, vect5, vect6)
 
print("Euclidean distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Euclidean distance between vectors using
# built in dist method By passing two-dimensional
# vector as a parameter Since we want to calculate
# Euclidean distance between each unique pair of vectors
# That is why we are passing Euclidean as a method
dist(twoDimensionalVect, method = "euclidean", diag = TRUE, upper = TRUE)


R
# R program to illustrate how to calculate
# Manhattan distance
# using dist() function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3,
                            vect4, vect5, vect6)
 
print("Manhattan distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Manhattan distance between vectors using
# built in dist method By passing two-dimensional
# vector as a parameter Since we want to calculate
# Manhattan distance between each unique pair of vectors
# That is why we are passing Manhattan as a method
dist(twoDimensionalVect, method = "manhattan", diag = TRUE, upper = TRUE)


R
# R program to illustrate how to calculate Maximum distance
# using dist() function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3, vect4, vect5, vect6)
 
print("Maximum distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Maximum distance between vectors using
# built in dist method By passing two-dimensional
# vector as a parameter Since we want to calculate
# Maximum distance between each unique pair of vectors
# That is why we are passing Maximum as a method
dist(twoDimensionalVect, method = "maximum", diag = TRUE, upper = TRUE)


R
# R program to illustrate how to calculate
# Canberra distance
# using dist() function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3,
                            vect4, vect5, vect6)
 
print("Canberra distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Canberra distance between vectors using
# built in dist method By passing two-dimensional
# vector as a parameter Since we want to calculate
# Canberra distance between each unique pair of vectors
# That is why we are passing Canberra as a method
dist(twoDimensionalVect, method = "canberra", diag = TRUE, upper = TRUE)


R
# R program to illustrate how to calculate
# Binary distance
# using dist() function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3,
                            vect4, vect5, vect6)
 
print("Binary distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Binary distance between vectors using
# built in dist method By passing two-dimensional
# vector as a parameter Since we want to calculate
# Binary distance between each unique pair of vectors
# That is why we are passing Binary as a method
dist(twoDimensionalVect, method = "binary", diag = TRUE, upper = TRUE)


R
# R program to illustrate how to calculate
# Minkowski distance
# using dist() function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3,
                            vect4, vect5, vect6)
 
print("Minkowski distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Minkowski distance between vectors using
# built in dist method By passing two-dimensional
# vector as a parameter Since we want to calculate
# Minkowski distance between each unique pair of vectors
# That is why we are passing Minkowski as a method
dist(twoDimensionalVect, method = "minkowski", diag = TRUE, upper = TRUE p = 2)


输出:

曼哈顿距离

曼哈顿距离是 N 维向量空间中两点之间的距离度量。它被定义为相应维度中坐标之间的绝对距离之和。例如,在具有两个点 Point1 (x 1 , y1) 和 Point2 (x 2 , y 2 ) 的二维空间中,曼哈顿距离由 |x 1 – x 2 | 给出。 + |y 1 – y 2 |。

在 R 中,曼哈顿距离是根据向量计算的。两个向量之间的曼哈顿距离由下式给出,

Σ|vect1i - vect2i| 

在哪里,

  • vect1 是第一个向量
  • vect2 是第二个向量

例如,给定两个向量,vect1 为 (3, 6, 8, 9),vect2 为 (1, 7, 8, 10)。他们的曼哈顿距离由 |3 – 1| 给出+ |6 – 7| + |8 – 8| + |9 – 10|等于 4。

句法:

示例:曼哈顿距离

R

# R program to illustrate how to calculate
# Manhattan distance
# using dist() function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3,
                            vect4, vect5, vect6)
 
print("Manhattan distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Manhattan distance between vectors using
# built in dist method By passing two-dimensional
# vector as a parameter Since we want to calculate
# Manhattan distance between each unique pair of vectors
# That is why we are passing Manhattan as a method
dist(twoDimensionalVect, method = "manhattan", diag = TRUE, upper = TRUE)

输出:

最大距离

两个向量 A 和 B 之间的最大距离计算为任何成对元素之间的最大差异。在 R 中,最大距离是相对于向量计算的。两个向量之间的最大距离由下式给出,

max(|vect1i - vect2i|) 

在哪里,

  • vect1 是第一个向量
  • vect2 是第二个向量

例如,给定两个向量,vect1 为 (3, 6, 8, 9),vect2 为 (1, 8, 9, 10)。它们的最大距离由等于 2 的 max(|3 – 1|, |6 – 8|, |8 – 9|, |9 – 10|) 给出。

句法:

示例:最大距离

R

# R program to illustrate how to calculate Maximum distance
# using dist() function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3, vect4, vect5, vect6)
 
print("Maximum distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Maximum distance between vectors using
# built in dist method By passing two-dimensional
# vector as a parameter Since we want to calculate
# Maximum distance between each unique pair of vectors
# That is why we are passing Maximum as a method
dist(twoDimensionalVect, method = "maximum", diag = TRUE, upper = TRUE)

输出:

堪培拉距离

堪培拉距离是向量空间中点对之间距离的数值度量。在 R Canberra 中,距离是根据向量计算的。两个向量之间的堪培拉距离由下式给出,

∑ |vect1i - vect2i| / (|vect1i| + |vect2i|) 

在哪里,

  • vect1 是第一个向量
  • vect2 是第二个向量

例如,给定两个向量,vect1 为 (2, 2, 7, 5),vect2 为 (3, 8, 3, 5)。他们的堪培拉距离由 |2 – 3| 给出/ (2 + 3) + |2 – 8| / (2 + 8) + |7 – 3| / (7 + 3) + |5 – 5| / (5 + 5) = 0.2 + 0.6 + 0.4 + 0 等于 1.2。

句法:

示例:堪培拉距离

R

# R program to illustrate how to calculate
# Canberra distance
# using dist() function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3,
                            vect4, vect5, vect6)
 
print("Canberra distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Canberra distance between vectors using
# built in dist method By passing two-dimensional
# vector as a parameter Since we want to calculate
# Canberra distance between each unique pair of vectors
# That is why we are passing Canberra as a method
dist(twoDimensionalVect, method = "canberra", diag = TRUE, upper = TRUE)

输出:

二进制距离

两个向量 A 和 B 之间的二进制距离计算为两个向量共享的元素的比例。

这里,

  • vect1 是第一个向量
  • vect2 是第二个向量

句法:

示例:二进制距离

R

# R program to illustrate how to calculate
# Binary distance
# using dist() function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3,
                            vect4, vect5, vect6)
 
print("Binary distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Binary distance between vectors using
# built in dist method By passing two-dimensional
# vector as a parameter Since we want to calculate
# Binary distance between each unique pair of vectors
# That is why we are passing Binary as a method
dist(twoDimensionalVect, method = "binary", diag = TRUE, upper = TRUE)

输出:

闵可夫斯基距离

Minkowski 距离是 N 维空间中两点之间的距离。它是欧几里得距离和曼哈顿距离的推广。例如,在具有两个点 Point1 (x 1 , y 1 ) 和 Point2 (x 2 , y 2 ) 的二维空间中,Minkowski 距离由 (|x 1 – y 1 | p + |x 2 – y 2 | p ) 1/p 。在 R 中,闵可夫斯基距离是相对于向量计算的。两个向量之间的 Minkowski 距离由下式给出,

(Σ|vect1i - vect2i|p)1/p

在哪里,

  • vect1 是第一个向量
  • vect2 是第二个向量
  • p 是一个整数

R 提供了一个内置的 dist() 方法来计算二维向量中每对向量之间的 Minkowski 距离。

句法:

例如,给定两个向量,vect1 为 (3, 6, 8, 9),vect2 为 (2, 7, 7, 10)。它们的 Minkowski 距离由 (|3 – 2| 2 + |6 – 7| 2 + |8 – 7| 2 + |9 – 10| 2 ) 1/2给出,等于 2。

示例:闵可夫斯基距离

R

# R program to illustrate how to calculate
# Minkowski distance
# using dist() function
 
# Initializing a vector
vect1 <- c(1, 4, 8, 9, 2, 3)
 
# Initializing another vector
vect2 <- c(9, 4, 1, 2, 4, 7)
 
# Initializing another vector
vect3 <- c(1, 7, 9, 3, 2, 8)
 
 
# Initializing another vector
vect4 <- c(2, 1, 4, 7, 8, 9)
 
 
# Initializing another vector
vect5 <- c(1, 4, 8, 3, 9, 2)
 
# Initializing another vector
vect6 <- c(3, 7, 8, 6, 5, 9)
 
 
#Row bind vectors into a single matrix
twoDimensionalVect <- rbind(vect1, vect2, vect3,
                            vect4, vect5, vect6)
 
print("Minkowski distance between each pair of vectors is: ")
cat("\n\n")
 
# Calculate Minkowski distance between vectors using
# built in dist method By passing two-dimensional
# vector as a parameter Since we want to calculate
# Minkowski distance between each unique pair of vectors
# That is why we are passing Minkowski as a method
dist(twoDimensionalVect, method = "minkowski", diag = TRUE, upper = TRUE p = 2)

输出: