Skip to contents

The Hamming distance of two vectors corresponds to the number of positions where the values differ.

Usage

hamming_dist(x, y, norm = FALSE)

x %HD% y

x %NHD% y

Arguments

x

A numeric or logical vector

y

A numeric or logical vector

norm

If TRUE normalize the distance to the vector length. By default it's FALSE

Value

The Hamming distance

Details

NAs are discarded in any of the vectors by using na.rm = TRUE in sum.

Examples

hamming_dist(c(0, 1, 0), c(0, 0, 0))
#> [1] 1
hamming_dist(c(0, 1, 0), c(0, 0, 0), norm = TRUE)
#> [1] 0.3333333

## NAs in any of the vectors are discarded
hamming_dist(c(1, NA, 3, 4, 5), c(1, 2, NA, 4, 5))
#> [1] 0
c(0, 1, 0) %HD% c(1, 0, 0)
#> [1] 2
c(0, 1, 0) %NHD% c(1, 0, 0)
#> [1] 0.6666667