geometric_mean: Geometric mean of a vector

View source: R/utils.R

geometric_meanR Documentation

Geometric mean of a vector

Description

Calculate the geometric mean of a vector. This function is valid for non-negative, non-NA containing vectors.

Usage

geometric_mean(x, zero_propagate = FALSE, ignore_zero = TRUE)

Arguments

x

numeric vector of non-negative values

zero_propagate

logical. Should zeros be included in the calculation. Default FALSE.

ignore_zero

logical. Should zero values be ignored in the calculation of the mean? Default TRUE.

Value

geometric mean of the vector

Examples

# Normal case
geometric_mean(c(2, 5, 95, 5))

# Default ignores 0s entirely
geometric_mean(c(2, 5, 95, 5, 0, 0, 0, 0))

# Ignore zero = FALSE -- zero is used in mean calculation
geometric_mean(c(2, 5, 95, 5, 0, 0, 0, 0), ignore_zero = FALSE)

# Case with NA -- Returns NA
geometric_mean(c(NA, 1, 2, 3))

# Case with 0 propagation -- Returns 0
geometric_mean(c(0, 1, 2, 3), zero_propagate = TRUE)

# Case with negative -- Returns NaN
geometric_mean(c(-1, 2, 3))

jcalendo/coriell documentation built on March 5, 2025, 5:42 a.m.