I tried to find a built-in for geometric mean but couldn\'t.
(Obviously a built-in isn\'t going to save me any time while working in the shell, nor do I suspect ther
The
exp(mean(log(x)))
will work unless there is a 0 in x. If so, the log will produce -Inf (-Infinite) which always results in a geometric mean of 0.
One solution is to remove the -Inf value before calculating the mean:
geo_mean <- function(data) {
log_data <- log(data)
gm <- exp(mean(log_data[is.finite(log_data)]))
return(gm)
}
You can use a one-liner to do this but it means calculating the log twice which is inefficient.
exp(mean(log(i[is.finite(log(i))])))
We can use psych package and call geometric.mean function.
No, but there are a few people who have written one, such as here.
Another possibility is to use this:
exp(mean(log(x)))