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

Sorkin, John j@ork|n @end|ng |rom @om@um@ry|@nd@edu
Thu May 12 22:55:00 CEST 2022

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!
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.

While the help file for mean explains that mean expects its parameters to be a vector, 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? If the parameter is not a vector, an error message might be generated. Without this, someone unsuspecting user (who does not check and think about results) might blindly accept the output of mean, use it to build a bridge, or skyscraper, which fails and result in the death of innocent victims. 


From: Richard M. Heiberger <rmh using temple.edu>
Sent: Thursday, May 12, 2022 3:52 PM
To: Sorkin, John; r-help using r-project.org
Subject: Re: [External] [R] result of mean(v1, v2, v3) of three real number  not the same as sum(v1, v2, v3)/3

you wrote

you intended to write
From: R-help <r-help-bounces using r-project.org> on behalf of Sorkin, John <jsorkin using som.umaryland.edu>
Sent: Thursday, May 12, 2022 3:31:51 PM
To: r-help using r-project.org <r-help using r-project.org>
Subject: [External] [R] result of mean(v1, v2, v3) of three real number not the same as sum(v1, v2, v3)/3

I have a very strange problem. I am getting different results from

> mean(mlagFZ1,mlagFZ2,mlagFZ3)
[1] -0.3326792

>  sum(mlagFZ1,mlagFZ2,mlagFZ3)/3
[1] -0.201942

R code:

> print(mlagFZ1)
[1] -0.3326792
> print(mlagFZ2)
[1] -0.1890601
> print(mlagFZ3)
[1] -0.0840866
>  sum(mlagFZ1,mlagFZ2,mlagFZ3)/3
[1] -0.201942
> mean(mlagFZ1,mlagFZ2,mlagFZ3)
[1] -0.3326792

Can someone tell me what I did wrong?
R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
PLEASE do read the posting guide https://nam10.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Crmh%40temple.edu%7Cad57db1be49d481a7b8b08da344e1ce5%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637879807634239778%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=h7unlucyqKJ2yWZ2nqVhZWekHPRq%2FbeTBEy1OSKIfZ0%3D&reserved=0<https://nam11.safelinks.protection.outlook.com/?url=http%3A%2F%2Fwww.r-project.org%2Fposting-guide.html&data=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdf1463e5f1f04d96525408da34510361%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637879819908301628%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=0FD7GgnSphuw1Ha419vFHTDw3aGPIjXrwPigrMkMfOU%3D&reserved=0>
and provide commented, minimal, self-contained, reproducible code.

More information about the R-help mailing list