To calculate hamming distance in R, you can use sum() function along with inequality operator !=.

The following method shows how you can do it with syntax.

Method: Use sum() Function and inequality operator

sum(vector1!=vector2)

The following example shows how to calculate hamming distance in R.

Calculate Hamming Distance Between Two Vectors

Let’s see how we can calculate hamming distance between two vectors:

# Define binary vector
x <- c(0,1,1,0,1,0)
y <- c(0,0,1,1,1,0)

# Calculate hamming distance
sum(x!=y)

Output:

[1] 2

Here the output hamming distance between two vectors is 2.