gm_mean: Geometric Mean

View source: R/gm_mean.R

gm_meanR Documentation

Geometric Mean

Description

Calculates the geometric mean of a vector

Usage

gm_mean(x, na.rm = TRUE, zero.propagate = FALSE)

Arguments

x

numeric vector of positive numbers.

na.rm

Remove NAs befor calculation (as in mean)

zero.propagate

Logical. Should zeros be considered (resulting in output of zero)

References

From stackoverflow answer posted by Paul McMurdie

Examples

### simple usage
gm_mean(c(1:4))
gm_mean(c(-1:4)) # negative values not allowed
gm_mean(c(0:4)) # zeros do not propagate
gm_mean(c(0:4), zero.propagate=TRUE) #zeros allowed to propagate
gm_mean(c(1,2,3,4, NaN)) # na.rm=TRUE
gm_mean(c(1,2,3,4, NaN), na.rm=FALSE) # na.rm=FALSE

### example of proportional change
df <- data.frame(index1 = 5, index2 = 25) # two indices of differing magnitude
mult <- c(1.25, 1.5) # multiplier
df <- rbind(df, df*mult) # indices change by differing proportions
df # view dataframe
gm_mean(mult) # mean proportional increase
gm_mean(df[2,]) / gm_mean(df[1,]) # equal
gm_mean(df[2,] / df[1,]) # equal


marchtaylor/sinkr documentation built on July 4, 2022, 5:48 p.m.