The Hamming weight is the number of non null symbols in a vector.
For a binary vector, it corresponds to the number of 1s.
Usage
hamming_weight(v, norm = FALSE)
Arguments
- v
A logical or numeric vector
- norm
If TRUE
(default is FALSE
) normalize the vector
Details
NAs are discarded in the vector by using na.rm = TRUE
in sum
.
Examples
## Weight of the vector
hamming_weight(c(0, 1, 1))
#> [1] 2
## Normalized weight
hamming_weight(c(0, 1, 1), norm = TRUE)
#> [1] 0.6666667
## `NA` are discarded
hamming_weight(c(1, 0, NA), norm = TRUE)
#> [1] 0.5