0

I'd like to calculate the geometric mean of long vectors of numbers, but I often seem to be limited by the size of numbers that R can store. To calculate a geometric mean, one has to find the product of all the numbers, which can be a gigantic number if the vector and/or the numbers are sufficiently large, and R seems to run out of space for it, e.g.:

> prod(sample(3,1000,replace=T))
[1] 2.09439e+265

is ok, but

> prod(sample(10,1000,replace=T))
[1] Inf

is not.

Is there a workaround, or do have to either A) settle for an approximation for the geometric mean (mean(x) - var(x)/2 being a widely-cited but rather terrible approximation), or B) export the data and use some other software such as Mathematica to do the job?

Many thanks

agentp
  • 6,849
  • 2
  • 19
  • 37
lukeholman
  • 595
  • 6
  • 14
  • 1
    http://stackoverflow.com/questions/2602583/geometric-mean-is-there-a-built-in might be useful – thelatemail Feb 23 '15 at 00:07
  • Hey there, I had read that but not carefully enough. For some reason I don't quite understand, exp(mean(log(x))) gives the same answer as prod(x)^(1/length(x)) - and of course it does not hit the number ceiling like the latter does. I was sick during school when they taught logs, and I've never totally got them! Thanks. – lukeholman Feb 23 '15 at 00:19

0 Answers0