Skip to contents

Function for calculating the geometric mean of a set of positive numbers.

Usage

geomean(x, na.rm = FALSE)

Arguments

x

A vector of positive numbers.

na.rm

a logical value indicating whether NA values should be stripped before the computation proceeds.

Value

If all values of x are positive, the geometric mean is returned as a numeric vector of length one. If any values are non-positive, NA is returned.

Examples

x <- c(1, 10, 100)
mean(x)
#> [1] 37
geomean(x)
#> [1] 10
geomean(c(-1,x))
#> Warning: All values must be positive to calculate the geometric mean.
#> [1] NA
geomean(c(0,x))
#> Warning: All values must be positive to calculate the geometric mean.
#> [1] NA
geomean(c(NA,x))
#> [1] NA
geomean(c(NA,x), na.rm=TRUE)
#> [1] 10