https://github.com/cran/robCompositions
Raw File
Tip revision: 325444901c2227c36bea9ac4bd62ffcee2b6bc95 authored by Matthias Templ on 06 May 2015, 17:22:25 UTC
version 1.9.1
Tip revision: 3254449
gm.R
#' geometric mean
#' 
#' This function calculates the geometric mean.
#' 
#' Calculates the geometric mean of all positive entries of a vector.
#' 
#' @param x A numeric vector.
#' @return The geometric mean.
#' @author Matthias Templ
#' @keywords math
#' @examples
#' 
#' gm(runif(100))
#' 
gm <- function (x) {
	if(!is.numeric(x)) stop("x has to be a vector of class numeric")
	if (any(na.omit(x == 0)))
		0
	else exp(mean(log(unclass(x)[is.finite(x) & x > 0])))
}
back to top