[R] [External] result of mean(v1, v2, v3) of three real number not the same as sum(v1, v2, v3)/3

Bert Gunter bgunter@4567 @end|ng |rom gm@||@com
Thu May 12 23:45:50 CEST 2022


I believe you are still misunderstanding. Inline comments below.

On Thu, May 12, 2022 at 2:02 PM Sorkin, John <jsorkin using som.umaryland.edu> wrote:
>
> I thank Richard Heiberger, Marc Schwartz, Eric Berger, Ivan Krylov, and David Stevens for answering my question regarding different results obtained from mean(v1,v2,v3)) and sum(v1,v2,v3)/3
>
> I believe the explanations points out a  possibly dangerous aspect of the sum vs mean functions. Mean may be used improperly!

Well, **any** function "may be used improperly"! It's up to the user
to study and follow the documentation.

>
> The help file for
> sum says:
> . . . numeric or complex or logical vectors
>
> The help for mean says:
> x, An R object, Currently there are methods for numeric/logical vectors, date, date/time/ and interval objects.

You need to pay closer attention: mean() is a (S3) generic function
whose first argument, x, is an **object** that determines dispatch.
This object is not necessarily a vector -- that is,
inherits(x,"vector") need **not** be TRUE( and indeed is FALSE for
various methods. See methods('mean') ).  If you do not understand
this, you need to read up on R's S3 generic function mechanism.
>
>
> While the help file for mean explains that mean expects its parameters to be a vector,
**That is false. ** It is only true for the 'x' parameter for the
**default** method.

> it is reasonable for an R user to assume that mean works on a series of scalers, i.e. v1,v2,v3 rather than requiring a vector i.e. c(v1,v2,v3).
>
> Should the help file for mean be modified so that it checks to see if its parameter is a vector?

??  Based on what I noted above, this query seems misbegotten.
Moreover, of course, a help file doesn't check anything -- the
**function** would have to check its arguments (I assume that's what
you meant).

 -- Bert



More information about the R-help mailing list