The harmonic mean is defined as the reciprocal of the arithmetic mean of the reciprocals of the data, or 1 / mean(1 / x)
, where x
contains positive numbers. Write a function harmonic_mean()
with arguments x
and na.rm = FALSE
that gives appropriate feedback:
Coercing 'x' to be numeric
if x
is not a numeric vector and does the coercion.'x' contains non-positive values
if there are non-positive values in x
. Hint: R’s function any()
can also take the argument na.rm
.x
in any other case, controlling for the argument na.rm
.
(from: Cotton, 2013, Learning R, O’Reilly)