Skip to contents

The geometric mean is the nth root of n products or e to the mean log of x.

Usage

geometricMean(x, ...)

# S4 method for AsIs
geometricMean(x)

# S4 method for matrix
geometricMean(x, MARGIN = 2L)

# S4 method for numeric
geometricMean(x, removeNa = TRUE, zeroPropagate = FALSE)

Arguments

x

Object.

MARGIN

integer(1-2). Dimension where the function will be applied. For a two-dimensional matrix: 1 indicates rows; 2 indicates columns; c(1, 2) indicates rows and columns.

removeNa

logical(1). Remove NA values from calculations.

zeroPropagate

logical(1). Allow propagation of zeroes.

...

Additional arguments.

Value

numeric.

Details

This function should be fully zero- and NA-tolerant. This calculation is not particularly useful if there are elements that are <= 0 and will return NaN.

Note

Updated 2021-10-14.

Examples

## numeric ====
vec1 <- seq(from = 1L, to = 5L, by = 1L)
print(vec1)
#> [1] 1 2 3 4 5
geometricMean(vec1)
#> [1] 2.605171

vec2 <- vec1^2L
print(vec2)
#> [1]  1  4  9 16 25
geometricMean(vec2)
#> [1] 6.786916

## matrix ====
matrix <- matrix(
    data = c(vec1, vec2),
    ncol = 2L,
    byrow = FALSE
)
print(matrix)
#>      [,1] [,2]
#> [1,]    1    1
#> [2,]    2    4
#> [3,]    3    9
#> [4,]    4   16
#> [5,]    5   25
geometricMean(matrix)
#> [1] 2.605171 6.786916