14.5 C
London
Thursday, May 1, 2025
HomeRDescriptive Statistics in RHow to Calculate Hamming Distance in R (With Examples)

How to Calculate Hamming Distance in R (With Examples)

Related stories

Learn About Opening an Automobile Repair Shop in India

Starting a car repair shop is quite a good...

Unlocking the Power: Embracing the Benefits of Tax-Free Investing

  Unlocking the Power: Embracing the Benefits of Tax-Free Investing For...

Income Splitting in Canada for 2023

  Income Splitting in Canada for 2023 The federal government’s expanded...

Can I Deduct Home Office Expenses on my Tax Return 2023?

Can I Deduct Home Office Expenses on my Tax...

Canadian Tax – Personal Tax Deadline 2022

  Canadian Tax – Personal Tax Deadline 2022 Resources and Tools...

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 #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 #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 #find Hamming distance between vectors
sum(x != y)

[1] 3

The Hamming distance between the two vectors is 1.

Additional Resources

How to Calculate Euclidean Distance in R
How to Calculate Manhattan Distance in R
How to Calculate Minkowski Distance in R
How to Calculate Mahalanobis Distance in R

Subscribe

- Never miss a story with notifications

- Gain full access to our premium content

- Browse free from up to 5 devices at once

Latest stories