gm_mean | R Documentation |
Calculates the geometric mean of a vector
gm_mean(x, na.rm = TRUE, zero.propagate = FALSE)
x |
numeric vector of positive numbers. |
na.rm |
Remove NAs befor calculation (as in |
zero.propagate |
Logical. Should zeros be considered (resulting in output of zero) |
From stackoverflow answer posted by Paul McMurdie
### 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
Add the following code to your website.
For more information on customizing the embed code, read Embedding Snippets.