R/harmonic_mean.R

Defines functions harmonic_mean

Documented in harmonic_mean

#' This is a quick and dirty harmonic mean calculation
#' 
#' This estimates the harmonic mean.
#' @param x a vector of values
#' @return The harmonic mean
#' @export
#' @examples 
#'  x <- c(1,2,3,4,5)
#'  harmonic_mean(x)
#'  x <- c(1,2,3,4,5,0)
#'  harmonic_mean(x)
harmonic_mean <- function(x) {
  if( any(x==0) ) {
    warning("Estimating harmonic mean with a zero, this is an undefined operation.")
  }
  return( 1/mean(1/x) )
} 
dyerlab/gstudio documentation built on Feb. 2, 2024, 8:24 p.m.