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

geometricMean(object, ...)

# S4 method for numeric
geometricMean(object, removeNA = TRUE, zeroPropagate = FALSE)

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

# S4 method for sparseMatrix
geometricMean(object, MARGIN = 2L)

## Arguments

object Object. logical(1). Remove NA values from calculations. logical(1). Allow propagation of zeroes. a vector giving the subscripts which the function will be applied over. E.g., for a matrix 1 indicates rows, 2 indicates columns, c(1, 2) indicates rows and columns. Where X has named dimnames, it can be a character vector selecting dimension names. Additional arguments.

## Value

numeric.

## Details

Useful for describing non-normal (i.e. geometric) distributions.

## Note

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.

Updated 2019-07-28.

• psych::geometric.mean().

## Examples

## numeric ====
vec1 <- seq(1L, 5L, 1L)
print(vec1)#>  1 2 3 4 5geometricMean(vec1)#>  2.605171
vec2 <- vec1 ^ 2L
print(vec2)#>   1  4  9 16 25geometricMean(vec2)#>  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   25geometricMean(matrix)#>  2.605171 6.786916
## sparseMatrix ====
sparse <- as(matrix, "sparseMatrix")
print(sparse)#> 5 x 2 sparse Matrix of class "dgCMatrix"
#>
#> [1,] 1  1
#> [2,] 2  4
#> [3,] 3  9
#> [4,] 4 16
#> [5,] 5 25geometricMean(sparse)#>  2.605171 6.786916