Modify the harmonic_mean()
function so that the return value has the class harmonic
. Now write an S3 print()
method for this class that displays the message The harmonic mean is Y
where Y
is the value computed by the function harmonic_mean()
, e.g.:
> harmonic_mean(x = c(1, 2, 4))
The harmonic mean is 1.714286
>
Hint: You will need the function cat()
in the S3 method.
(from: Cotton, 2013, Learning R, O’Reilly)