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

Avi Gross @v|gro@@ @end|ng |rom ver|zon@net
Fri May 13 01:17:55 CEST 2022

Good point, Bert. On my machine, methods("mean") returns 8 variations including for Dates.
But this being R, if you LIKE having a version of mean() that does it your way, go for it!

mymean <- function(...) mean(c(...))

The function defined above takes any number of arguments and makes a vector of them and passes it to mean() but note will not handle arguments about trim or na.rm.


[1] 2.5

There are subtle errors if you try. using na.rm=TRUE has a term evaluated to 1. Using trim=8 so you cannot mix and match just because you feel like it. Yes, there are ways to evaluate the arguments and package some into a c() and pass along others but my point is what others keep saying. Don't assume a function can be used the way you want it to but the way the manual page says it can be used. Sometimes, though, you can make your own function for personal use that does what you want. You don't even have to piggyback like my example. Your function can take any number of arguments, evaluate each one to see if it seems numeric and has length 1 and is not NA, calculate how many it got that are valid,  add them together in a loop and divide by the total.
But you may not want to name it mean() unless ...
-----Original Message-----
From: Bert Gunter <bgunter.4567 using gmail.com>
To: Sorkin, John <jsorkin using som.umaryland.edu>
Cc: r-help using r-project.org <r-help using r-project.org>; Schwartz, Marc (MSchwartz using mn.rr.com) <mschwartz using mn.rr.com>
Sent: Thu, May 12, 2022 5:45 pm
Subject: Re: [R] [External] result of mean(v1, v2, v3) of three real number not the same as sum(v1, v2, v3)/3

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

R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

	[[alternative HTML version deleted]]

More information about the R-help mailing list