gm_mean: Calculate the geometric mean

View source: R/geometricMean.R

gm_meanR Documentation

Calculate the geometric mean

Description

gm_mean takes as input a numeric vector and returns the geometric mean

Usage

gm_mean(x, na.rm = TRUE, zero.propagate = FALSE)

Arguments

x

A vector of numbers

na.rm

Should NA values be removed? (logical)

zero.propagate

Should zeroes be propagated? (logical)

Details

When there are no zeroes in a vector of numbers, then the geometric mean is defined as the nth root of the product of all the values and is equivalently defined as exp(1/n * the sum of the logs of each value). However, those two are not equivalent if there are zeroes!

This function gives the option of propagating zeroes. If you want to get a 0 when the vector includes 0 values, that's the definition of the nth root of the product of all values, so that would be zero.propagate = TRUE.

Alternatively, if you want to just ignore 0 values, you can choose zero.propagate = FALSE. Then, either definition works, and, since R has a built-in sum(...) function but not a built-in product(...) function, you use the definition where the geometric mean = exp(1/n * the sum of the logs of each value) and that will give you an actual number if you ignore the zeroes.

Note that the geometric mean is undefined for negative numbers.

Value

Returns a number

Examples

gm_mean(rnorm(10, 5, 1))


shirewoman2/LaurasHelpers documentation built on Oct. 22, 2023, 2:07 p.m.