In this article, we will be looking at various methods to calculate the hamming distance in the R programming language.
Hamming distance between two data collections is the number of positions at which corresponding elements are different. In other words, we can say that the minimum number of changes is required to make two data collections identical. Hamming distance in R is computed with respect to vectors.

Numeric vectors: A numeric vector is a vector that consists of the integer, decimal, double, etc values. Consider an example we are given two numeric vectors, vect1 = c(1, 3, 4, 5) and vect2 = c(1, 3, 9, 5) as only the corresponding third element of vectors are different so the Hamming distance is equal to one.
In R, we can calculate Hamming distance between two numeric vectors by following the below methods:
Method 1: Using inbuilt sum() function to calculate Hamming distance of the numeric vector
In this method, the user needs to call an inbuilt sum function using which we can compute Hamming distance between numeric vectors. Internally, this function computes the number of corresponding positions at which the element of vect1 is not equal to the element of vect2.
Syntax:
sum(vect1 != vect2)
Example:
Lets us consider two numeric vectors, vect1 = c(10, 2, 3, 7, 8, 12) and vect2 = c(10, 2, 1, 2, 0, 24). Clearly, corresponding third, fourth, fifth and sixth elements are different. Hence, Hamming distance is equal to 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))
Output:
[1] "Hamming distance between vect1 and vect2 is equal to 4"
Method 2: Using a custom() function to calculate Hamming distance of the numeric vector
In this approach, the user can create their own custom function which computes the Hamming distance between two numeric vectors. Follow the steps below to calculate Hamming distance using a custom function.
- Define a computerHammingDistance function. It accepts two vectors as parameters, vect1 and vect2.
- Initialize a variable answer as 0, it calculates the final answer.
- Now iterate over the length of the vect1 or vect2 using a for-in loop. If vect1[index] is equal to vect2[index] then increment the answer variable by one.
- After the end of iteration return the answer variable from the function.
- Initialize a variable HammingDistance and assign the value returned by the function to it.
- Print the value represented by the HammingDistance variable.
Example:
Lets us consider two numeric vectors, vect1 = c(11, 7, 3, 2, 8, 12) and vect2 = c(8, 7, 3, 2, 8, 24). Clearly, the corresponding first and last elements are different. Hence, Hamming distance is equal to two.
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))
Output:
[1] "Hamming distance between vect1 and vect2 is equal to 2"
Method 3: Using inbuilt sum() function to calculate Hamming distance of binary vector
In this approach, the user needs to call an inbuilt sum function using which we can compute Hamming distance between two binary vectors.
Binary vectors: A binary vector is a vector that consists of values having only two logical values, 0 or 1. Consider an example we are given two binary vectors, vect1 = c(1, 0, 1, 0) and vect2 = c(1, 1, 1, 1). As only the corresponding second and fourth elements of vectors are different so the Hamming distance is equal to two.
Syntax:
sum(vect1 != vect2)
Internally, this function computes the number of corresponding positions at which the element of vect1 is not equal to the element of vect2.
Example:
Lets us consider two binary vectors, vect1 = c(0, 1, 0, 1, 0, 1) and vect2 = c(1, 1, 0, 0, 1, 1). Clearly, corresponding first, fourth and fifth elements are different. Hence, Hamming distance is equal to three.
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))
Output:
[1] "Hamming distance between vect1 and vect2 is equal to 3"
Method 4: Using a custom() function to calculate Hamming distance of the binary vector
In this method, the user has to follow the same approach as shown above in method 2 with just changing the vector type to binary in the R programming language.
Example:
Lets us consider two binary vectors, vect1 = c(0, 1, 0, 1, 1, 1) and vect2 = c(1, 1, 0, 0, 1, 1). Clearly, corresponding first and fourth elements are different. Hence, Hamming distance is equal to two.
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))
Output:
[1] "Hamming distance between vect1 and vect2 is equal to 2"
Method 5: Using inbuilt sum() function to calculate Hamming distance of string vector
Under this approach, the user has to apply the same approach as shown above in method-1 and method-3 by changing the vector type to string.
String vector: A string vector is a vector that consists of a number of strings. Consider an example we are given two numeric vectors, vect1 = c("GeeksforGeeks", "R", "C++", "Java") and vect2 = c("Geeks", "R", "C", "Java") as only the corresponding first and third elements of vectors are different, hence the Hamming distance is equal to two.
Syntax:
sum(vect1 != vect2)
Example:
Lets us consider two string vectors, vect1 = c("GeeksforGeeks", "Java", "Python", "C++", "R", "Swift", "PHP") and vect2 = c("Geeks", "Java", "C++", "R", "Python", "Swift", "C#"). Clearly, corresponding first, third, fourth, fifth and seventh elements are different. Hence, Hamming distance is equal to five.
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))
Output:
[1] "Hamming distance between vect1 and vect2 is equal to 5"
Method 6: Using a custom() function to calculate Hamming distance of string vector
In this method, the user has to follow the same approach as shown above in method 2 and method 4 with just changing the vector type to string in the R programming language.
Example:
Lets us consider two string vectors, vect1 = c("GeeksforGeeks", "Java", "C#", "C++", "R", "PHP", "Swift") and vect2 = c("GeeksforGeeks", "Python", "C", "R", "Python", "Swift", "C#"). Clearly, corresponding second, third, fourth, fifth, sixth and seventh elements are different. Hence, Hamming distance is equal to six.
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))
Output:
[1] "Hamming distance between vect1 and vect2 is equal to 6"
Similar Reads
How to Calculate Euclidean Distance in R? Euclidean distance between two points in Euclidean space is the length of a line segment between the two points. It can be calculated from the Cartesian coordinates of the points using the Pythagorean theorem, therefore occasionally being called the Pythagorean distance. The Euclidean distance betwe
3 min read
How to Calculate Minkowski Distance in R? In this article, we are going to see how to calculate Minkowski Distance in the R Programming language. Minkowski distance:Â Minkowski distance is a distance measured between two points in N-dimensional space. It is basically a generalization of the Euclidean distance and the Manhattan distance. It
6 min read
How to Calculate Levenshtein Distance in R? In this article, we will discuss how to calculate Levenshtein Distance in the R Programming Language. The Levenshtein distance between two strings is the minimum number of character substitutions, insertions, and deletions required to turn one string into the other string. The Levenshtein distance
3 min read
How to Calculate Mahalanobis Distance in R? In this article, we are going to calculate Mahalanobis distance in R Programming Language. Mahalanobis distance is used to calculate the distance between two points or vectors in a multivariate distance metric space which is a statistical analysis involving several variables. To start with we need a
3 min read
How to Calculate Manhattan Distance in R? Manhattan distance is a distance metric between two points in an N-dimensional vector space. It is defined as the sum of absolute distance between coordinates in corresponding dimensions. For example, In a 2-dimensional space having two points Point1 (x1,y1) and Point2 (x2,y2), the Manhattan distan
4 min read
How to Calculate Euclidean Distance in Excel? Euclidean distance is the distance between two real-valued vectors. It is calculated by the square root of the sum of the squared differences of the elements in the two vectors.The formula to calculate Euclidean distance is :In this article we are going to discuss how to calculate the Euclidean dist
2 min read