[R] Geometric mean of rows in matrix

David Winsemius dwinsemius at comcast.net
Thu Aug 20 20:45:30 CEST 2009


On Aug 20, 2009, at 2: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 ?

 > xx <- matrix(10+rnorm(100), 10)

 > apply(xx, 1, function(x) exp(mean(log(x))) )
  [1] 10.084711 10.034054  9.622909  9.780703 10.085467  9.800346  
10.283556  9.666453  9.802996
[10]  9.917821

This of course will return NaN if there are any negative values. There  
are also several implementations of geometric mean functions that can  
be found with the obvious search strategy at:

http://search.r-project.org/nmz.html

-- 
David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list