# How to Calculate Hamming Distance in R (With Examples)

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)

 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)

 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)

 3
```

The Hamming distance between the two vectors is 1.