[R] Geometric mean of rows in matrix

Benilton Carvalho bcarvalh at jhsph.edu
Thu Aug 20 20:38:38 CEST 2009


set.seed(1)
x <- matrix(runif(1000), 100)
system.time(tmp1 <- exp(rowMeans(log(x))))
system.time(tmp2 <- apply(x, 1, function(y) prod(y)^(1/length(y))))
all.equal(tmp1, tmp2)

## tmp1 is more robust, btw

On Aug 20, 2009, at 3:22 PM, Edward Chen wrote:

> Is there a function or an easier way to computer geometric means of  
> each
> rows in a nxn matrix and spit out in an 1xn matrix ?
>
> --
> Edward Chen
>
>        [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list