Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

finnstats:-For the latest Data Science, jobs and UpToDate tutorials visit finnstats

Hamming Distance in R, the total of matching elements that differ between two vectors is the Hamming distance between them.

A metric for comparing two vectors is hamming distance. Hamming distance is the number of bit positions in which the two bits differ when comparing two vectors of equal length.

d(a,b) represents the Hamming distance between two strings, a and b.

Let’s pretend we have the following two vectors:

```x = [1, 2, 6, 4]
y = [1, 2, 3, 7]```

Because the total number of matching elements with different values is 2, the Hamming distance between the two vectors will be 2.

In R, we can use the following syntax to calculate the Hamming distance between two vectors:

sum(x != y)

This tutorial shows you how to use this function in practice with various examples.

## Approach 1: Binary Hamming Distance

The code below explains how to calculate the Hamming distance between two vectors with only two potential values each.

Let’s create two vectors x and y

```x <- c(0, 1, 1, 1, 1)
y <- c(0, 1, 0, 0, 0)```

Now we can find the Hamming distance between the above vectors

`sum(x != y)`

 3

Between the two vectors, the Hamming distance is 3.

## Approach 2: Numerical Vectors Hamming Distance

The following code demonstrates how to calculate the Hamming distance between two vectors with multiple numerical values each:

Let’s create two numerical vectors

```x <- c(2, 15, 16, 25, 12)
y <- c(2, 12, 16, 25, 17)```

Yes, Now we can find the Hamming distance between vectors

`sum(x != y)`

 2

Between the two vectors, the Hamming distance is 2.

## Example 3: Hamming Distance String Vectors

The following code demonstrates how to calculate the Hamming distance between two vectors with multiple character values each.

Load string vectors into R console

```x <- c('aa', 'bb', 'cc', 'dd')
y <- c('aa', 'bb', 'cc', 'dd')```

find out the hamming distance between two string vectors

`sum(x != y)`

[ 0

Between the two vectors, the Hamming distance is 0.

Likelihood Ratio Test in R with Example »