How to Calculate Hamming Distance in R (With Examples)

The Hamming distance is a measure of the number of differing bits between two binary strings of equal length. In R, the hamdist() function can be used to calculate the Hamming distance between two strings. This function takes two character vectors of equal length and returns the number of different characters between them, with 0 indicating that the two strings are identical. Examples of using the hamdist() function in R are provided to help illustrate the concept.


The Hamming distance between two vectors is simply the sum of corresponding elements that differ between the vectors.

For example, suppose we have the following two vectors:

x = [1, 2, 3, 4]

y = [1, 2, 5, 7]

The Hamming distance between the two vectors would be 2, since this is the total number of corresponding elements that have different values.

To calculate the Hamming distance between two vectors in R, we can use the following syntax:

sum(x != y)

This tutorial provides several examples of how to use this function in practice.

Example 1: Hamming Distance Between Binary Vectors

The following code shows how to calculate the Hamming distance between two vectors that each contain only two possible values:

#create vectors
x <- c(0, 0, 1, 1, 1)
y <- c(0, 1, 1, 1, 0)

#find Hamming distance between vectors
sum(x != y)

[1] 2

The Hamming distance between the two vectors is 2.

Example 2: Hamming Distance Between Numerical Vectors

The following code shows how to calculate the Hamming distance between two vectors that each contain several numerical values:

#create vectors
x <- c(7, 12, 14, 19, 22)
y <- c(7, 12, 16, 26, 27)

#find Hamming distance between vectors
sum(x != y)

[1] 3

The Hamming distance between the two vectors is 3.

Example 3: Hamming Distance Between String Vectors

The following code shows how to calculate the Hamming distance between two vectors that each contain several character values:

#create vectors
x <- c('a', 'b', 'c', 'd')
y <- c('a', 'b', 'c', 'r')

#find Hamming distance between vectors
sum(x != y)

[1] 3

The Hamming distance between the two vectors is 1.

x