Skip to contents

The ratio is calculated as the number of 1s minus the number of 0s. A positive ratio indicates that there are more 1s than 0s, while a negative results indicates the opposite. NA are not accounted to calculate the length of the vector.

Usage

ratio_bits(v)

Arguments

v

A binary vector

Value

The ratio of bits in the binary vector

Details

By using the hamming_weight function, the ratio can be calculated in the following way. $$Ratio = \frac{HW(v) - (\#v - HW(v))}{\#v} = 2 \cdot HW_{norm}(v) - 1$$ The operator \(\#v\) denotes the number of elements in the vector \(v\).

See also

Examples

## Negative ratio
ratio_bits(c(0, 1, 0))
#> [1] -0.3333333

## Positive ratio
ratio_bits(c(1, 1, 0))
#> [1] 0.3333333

## `NA` are discarded
ratio_bits(c(1, 1, 0, NA, NA))
#> [1] 0.3333333