R/geometric_mean.R

Defines functions geometric.mean

Documented in geometric.mean

#' Geometric mean
#'
#' Calculates the geometric mean of a numeric vector.
#'
#' @param x A numeric vector
#' @param na.rm Logical. Should NA values be omitted from calculation? Default = T
#' @author Sean Rohan \email{sean.rohan@@noaa.gov}
#' @export

geometric.mean <- function(x, na.rm = T) {

  if(any(x <= 0)) {
    stop("geometric.mean: Cannot calculate a geometric mean. Input vector contains negative values.")
  }

  x <- x[!is.na(x)]

  return(exp(mean(log(x))))
}
sean-rohan-NOAA/trawllight documentation built on Dec. 31, 2022, 5:42 p.m.