Calculate the Harmonic Mean
calc_harmonic_mean.RdComputes the harmonic mean of a numeric vector. The harmonic mean is defined as \(n / \sum (1/x_i)\), where \(n\) is the number of observations.
Details
If
ignore.zero = FALSEand the vector contains zeros, the harmonic mean is zero by definition.If all values are removed (due to
NAs or zeros), the function returnsNA.
Examples
# Simple harmonic mean
calc_harmonic_mean(c(1, 2, 3))
#> [1] 1.636364
# With a zero (harmonic mean collapses to 0)
calc_harmonic_mean(c(1, 2, 0))
#> [1] 0
# Ignoring zeros
calc_harmonic_mean(c(1, 2, 0), ignore.zero = TRUE)
#> [1] 1.333333
# With NA values
calc_harmonic_mean(c(1, 2, NA), na.rm = TRUE)
#> [1] 1.333333
# Combining both options
calc_harmonic_mean(c(1, 2, 0, NA), na.rm = TRUE, ignore.zero = TRUE)
#> [1] 1.333333