To calculate the harmonic mean in R, you can use the mean()
function with the argument 1/x
to invert each element of your vector x
, then calculate the mean of these inverted values.
Here is an example code:
main.r135 chars9 lines
The output should be:
main.r13 chars2 lines
Note that the harmonic mean is often used for calculating the average rate or speed in situations where different rates are being combined.
gistlibby LogSnag