View source: R/geometricMean.R
gm_mean | R Documentation |
gm_mean
takes as input a numeric vector and returns the geometric mean
gm_mean(x, na.rm = TRUE, zero.propagate = FALSE)
x |
A vector of numbers |
na.rm |
Should NA values be removed? (logical) |
zero.propagate |
Should zeroes be propagated? (logical) |
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.
Returns a number
gm_mean(rnorm(10, 5, 1))
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.