如何计算R中的汉明距离?
在本文中,我们将研究在 R 编程语言中计算有害距离的各种方法。
两个数据集合之间的汉明距离是对应元素不同的位置数。换句话说,我们可以说使两个数据集合相同所需的更改次数最少。 R中的汉明距离是相对于向量计算的。
数字向量:数字向量是由整数、小数、双精度等值组成的向量。考虑一个例子,我们有两个数值向量,vect1 = c(1, 3, 4, 5) 和 vect2 = c(1, 3, 9, 5) 因为只有对应的向量的第三个元素不同所以汉明距离是等于一。
在 R 中,我们可以通过以下方法计算两个数值向量之间的汉明距离:
方法一:使用内置 sum()函数计算数值向量的汉明距离
在这种方法中,用户需要调用一个内置的求和函数,我们可以使用它来计算数值向量之间的汉明距离。在内部,此函数计算 vect1 的元素不等于 vect2 的元素的对应位置的数量。
句法:
sum(vect1 != vect2)
例子:
让我们考虑两个数值向量,vect1 = c(10, 2, 3, 7, 8, 12) 和 vect2 = c(10, 2, 1, 2, 0, 24)。显然,对应的第三、第四、第五和第六要素是不同的。因此,汉明距离等于 4。
R
# Swift program to illustrate the working
# of sum function to compute Hamming distance
# between numeric vectors
# Initializing a vector of integers
vect1 <- c(10, 2, 3, 7, 8, 12)
# Initializing another vector of Qintegers
vect2 <- c(10, 2, 1, 2, 0, 24)
# Hamming distance
HammingDistance = sum(vect1 != vect2)
# Print Hamming distance
print(paste("Hamming distance between vect1 and vect2 \
is equal to", HammingDistance))
R
# Swift program to illustrate the working of
# sum function to compute Hamming distance
# between numeric vectors
# Function to compute Hamming Distance
computeHammingDistance <- function(vect1, vect2) {
# Initialize answer as 0
# It calculates our Hamming distance
answer <- 0
# Iterate over the length of the vector
for (index in 1:length(vect1)) {
# If vect1[index] is not equal to vect2[index]
if (vect1[index] != vect2[index]){
# Update answer variable
# Increment the count
answer = answer + 1
}
}
# Return the calculated distance
return (answer)
}
# Initializing a vector
vect1 <- c(11, 7, 3, 2, 8, 12)
# Initializing another vector
vect2 <- c(8, 7, 3, 2, 8, 24)
# Call computeHammingDistance function
HammingDistance = computeHammingDistance(vect1, vect2)
# Print Hamming Distance
print(paste("Hamming distance between vect1 and vect2\
is equal to", HammingDistance))
R
# Swift program to illustrate the working of
# sum function to compute Hamming distance
# between binary vectors
# Initializing a binary vector
vect1 <- c(0, 1, 0, 1, 0, 1)
# Initializing another binary vector
vect2 <- c(1, 1, 0, 0, 1, 1)
# Hamming distance
HammingDistance = sum(vect1 != vect2)
# Print Hamming distance
print(paste("Hamming distance between vect1 and vect2 \
is equal to", HammingDistance))
R
# Swift program to illustrate the working of
# sum function to compute Hamming distance
# between binary vectors
# Function to compute Hamming Distance
computeHammingDistance <- function(vect1, vect2) {
# Initialize answer as 0
# It calculates our Hamming distance
answer <- 0
# Iterate over the length of the vector
for (index in 1:length(vect1)) {
# If vect1[index] is not equal to vect2[index]
if (vect1[index] != vect2[index]){
# Update answer variable
# Increment the count
answer = answer + 1
}
}
# Return the calculated distance
return (answer)
}
# Initializing a binary vector
vect1 <- c(0, 1, 0, 1, 1, 1)
# Initializing another binary vector
vect2 <- c(1, 1, 0, 0, 1, 1)
# Call computeHammingDistance function
HammingDistance = computeHammingDistance(vect1, vect2)
# Print Hamming Distance
print(paste("Hamming distance between vect1 and vect2 \
is equal to", HammingDistance))
R
# Swift program to illustrate the working of
# sum function to compute Hamming distance
# between string vectors
# Initializing a string vector
vect1 <- c("GeeksforGeeks", "Java", "Python", "C++",
"R", "Swift", "PHP")
# Initializing another string vector
vect2 <- c("Geeks", "Java", "C++", "R", "Python",
"Swift", "C#")
# Hamming distance
HammingDistance = sum(vect1 != vect2)
# Print Hamming distance
print(paste("Hamming distance between vect1 and vect2 is\
equal to", HammingDistance))
R
# Swift program to illustrate the working of
# sum function to compute Hamming distance
# between string vectors
# Function to compute Hamming Distance
computeHammingDistance <- function(vect1, vect2) {
# Initialize answer as 0
# It calculates our Hamming distance
answer <- 0
# Iterate over the length of the vector
for (index in 1:length(vect1)) {
# If vect1[index] is not equal to vect2[index]
if (vect1[index] != vect2[index]){
# Update answer variable
# Increment the count
answer = answer + 1
}
}
# Return the calculated distance
return (answer)
}
# Initializing a string vector
vect1 <- c("GeeksforGeeks", "Java", "C#", "C++", "R", "PHP", "Swift")
# Initializing another string vector
vect2 <- c("GeeksforGeeks", "Python", "C", "R", "Python", "Swift", "C#")
# Call computeHammingDistance function
HammingDistance = computeHammingDistance(vect1, vect2)
# Print Hamming Distance
print(paste("Hamming distance between vect1 and vect2 is equal to", HammingDistance))
输出:
[1] "Hamming distance between vect1 and vect2 is equal to 4"
方法二:使用custom()函数计算数值向量的汉明距离
在这种方法中,用户可以创建自己的自定义函数来计算两个数值向量之间的汉明距离。按照以下步骤使用自定义函数计算汉明距离。
- 定义一个计算机HammingDistance函数。它接受两个向量作为参数,vect1 和 vect2。
- 将变量 answer 初始化为 0,它计算最终答案。
- 现在使用 for-in 循环遍历 vect1 或 vect2 的长度。如果 vect1[index] 等于 vect2[index] 则将 answer 变量加一。
- 迭代结束后,从函数返回答案变量。
- 初始化一个变量 HammingDistance 并将函数返回的值赋给它。
- 打印由 HammingDistance 变量表示的值。
例子:
让我们考虑两个数值向量,vect1 = c(11, 7, 3, 2, 8, 12) 和 vect2 = c(8, 7, 3, 2, 8, 24)。显然,对应的第一个和最后一个元素是不同的。因此,汉明距离等于二。
R
# Swift program to illustrate the working of
# sum function to compute Hamming distance
# between numeric vectors
# Function to compute Hamming Distance
computeHammingDistance <- function(vect1, vect2) {
# Initialize answer as 0
# It calculates our Hamming distance
answer <- 0
# Iterate over the length of the vector
for (index in 1:length(vect1)) {
# If vect1[index] is not equal to vect2[index]
if (vect1[index] != vect2[index]){
# Update answer variable
# Increment the count
answer = answer + 1
}
}
# Return the calculated distance
return (answer)
}
# Initializing a vector
vect1 <- c(11, 7, 3, 2, 8, 12)
# Initializing another vector
vect2 <- c(8, 7, 3, 2, 8, 24)
# Call computeHammingDistance function
HammingDistance = computeHammingDistance(vect1, vect2)
# Print Hamming Distance
print(paste("Hamming distance between vect1 and vect2\
is equal to", HammingDistance))
输出:
[1] "Hamming distance between vect1 and vect2 is equal to 2"
方法 3:使用内置 sum()函数计算二进制向量的汉明距离
在这种方法中,用户需要调用一个内置的 sum函数,我们可以使用它来计算两个二进制向量之间的汉明距离。
二进制向量:二进制向量是由只有两个逻辑值 0 或 1 的值组成的向量。考虑一个例子,我们给出了两个二进制向量,vect1 = c(1, 0, 1, 0) 和 vect2 = c( 1、1、1、1)。由于只有向量的相应第二个和第四个元素不同,因此汉明距离等于 2。
句法:
sum(vect1 != vect2)
在内部,此函数计算 vect1 的元素不等于 vect2 的元素的对应位置的数量。
例子:
让我们考虑两个二进制向量,vect1 = c(0, 1, 0, 1, 0, 1) 和 vect2 = c(1, 1, 0, 0, 1, 1)。显然,对应的第一、第四和第五元素是不同的。因此,汉明距离等于三。
R
# Swift program to illustrate the working of
# sum function to compute Hamming distance
# between binary vectors
# Initializing a binary vector
vect1 <- c(0, 1, 0, 1, 0, 1)
# Initializing another binary vector
vect2 <- c(1, 1, 0, 0, 1, 1)
# Hamming distance
HammingDistance = sum(vect1 != vect2)
# Print Hamming distance
print(paste("Hamming distance between vect1 and vect2 \
is equal to", HammingDistance))
输出:
[1] "Hamming distance between vect1 and vect2 is equal to 3"
方法4:使用custom()函数计算二进制向量的汉明距离
在这种方法中,用户必须遵循与上面方法 2 相同的方法,只需将向量类型更改为 R 编程语言中的二进制。
例子:
让我们考虑两个二进制向量,vect1 = c(0, 1, 0, 1, 1, 1) 和 vect2 = c(1, 1, 0, 0, 1, 1)。显然,对应的第一和第四元素是不同的。因此,汉明距离等于二。
R
# Swift program to illustrate the working of
# sum function to compute Hamming distance
# between binary vectors
# Function to compute Hamming Distance
computeHammingDistance <- function(vect1, vect2) {
# Initialize answer as 0
# It calculates our Hamming distance
answer <- 0
# Iterate over the length of the vector
for (index in 1:length(vect1)) {
# If vect1[index] is not equal to vect2[index]
if (vect1[index] != vect2[index]){
# Update answer variable
# Increment the count
answer = answer + 1
}
}
# Return the calculated distance
return (answer)
}
# Initializing a binary vector
vect1 <- c(0, 1, 0, 1, 1, 1)
# Initializing another binary vector
vect2 <- c(1, 1, 0, 0, 1, 1)
# Call computeHammingDistance function
HammingDistance = computeHammingDistance(vect1, vect2)
# Print Hamming Distance
print(paste("Hamming distance between vect1 and vect2 \
is equal to", HammingDistance))
输出:
[1] "Hamming distance between vect1 and vect2 is equal to 2"
方法5:使用内置sum()函数计算字符串向量的汉明距离
在这种方法下,用户必须通过将向量类型更改为字符串来应用与上面方法 1 和方法 3 中所示相同的方法。
字符串向量:字符串向量是由多个字符串组成的向量。考虑一个例子,给定两个数值向量,vect1 = c(“GeeksforGeeks”, “R”, “C++”, “Java”) 和 vect2 = c(“Geeks”, “R”, “C”, “Java” ) 因为只有对应的向量的第一个和第三个元素不同,因此汉明距离等于 2。
句法:
sum(vect1 != vect2)
例子:
让我们考虑两个字符串向量,vect1 = c(“GeeksforGeeks”, “Java”, “Python”, “C++”, “R”, “Swift”, “PHP”) 和 vect2 = c(“Geeks”, “Java”、“C++”、“R”、“Python”、“Swift”、“C#”)。显然,对应的第一、第三、第四、第五和第七元素是不同的。因此,汉明距离等于五。
R
# Swift program to illustrate the working of
# sum function to compute Hamming distance
# between string vectors
# Initializing a string vector
vect1 <- c("GeeksforGeeks", "Java", "Python", "C++",
"R", "Swift", "PHP")
# Initializing another string vector
vect2 <- c("Geeks", "Java", "C++", "R", "Python",
"Swift", "C#")
# Hamming distance
HammingDistance = sum(vect1 != vect2)
# Print Hamming distance
print(paste("Hamming distance between vect1 and vect2 is\
equal to", HammingDistance))
输出:
[1] "Hamming distance between vect1 and vect2 is equal to 5"
方法6:使用custom()函数计算字符串向量的汉明距离
在此方法中,用户必须遵循与上面方法 2 和方法 4 中所示相同的方法,只需将向量类型更改为 R 编程语言中的字符串。
例子:
让我们考虑两个字符串向量,vect1 = c(“GeeksforGeeks”, “Java”, “C#”, “C++”, “R”, “PHP”, “Swift”) 和 vect2 = c(“GeeksforGeeks”, “Python”、“C”、“R”、“Python”、“Swift”、“C#”)。显然,对应的第二、第三、第四、第五、第六和第七元素是不同的。因此,汉明距离等于六。
R
# Swift program to illustrate the working of
# sum function to compute Hamming distance
# between string vectors
# Function to compute Hamming Distance
computeHammingDistance <- function(vect1, vect2) {
# Initialize answer as 0
# It calculates our Hamming distance
answer <- 0
# Iterate over the length of the vector
for (index in 1:length(vect1)) {
# If vect1[index] is not equal to vect2[index]
if (vect1[index] != vect2[index]){
# Update answer variable
# Increment the count
answer = answer + 1
}
}
# Return the calculated distance
return (answer)
}
# Initializing a string vector
vect1 <- c("GeeksforGeeks", "Java", "C#", "C++", "R", "PHP", "Swift")
# Initializing another string vector
vect2 <- c("GeeksforGeeks", "Python", "C", "R", "Python", "Swift", "C#")
# Call computeHammingDistance function
HammingDistance = computeHammingDistance(vect1, vect2)
# Print Hamming Distance
print(paste("Hamming distance between vect1 and vect2 is equal to", HammingDistance))
输出:
[1] "Hamming distance between vect1 and vect2 is equal to 6"