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

Jeff Newmiller jdnewm|| @end|ng |rom dcn@d@v|@@c@@u@
Thu May 12 23:30:48 CEST 2022


R is at heart used for processing vectors of data. I find the flexibility of sum to be more disturbing than the constraints of mean.

On May 12, 2022 1:55:00 PM PDT, "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!
>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. 
>
>John
>
>
>
>
>________________________________________
>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
> mean(mlagFZ1,mlagFZ2,mlagFZ3)
>
>you intended to write
> mean(c(mlagFZ1,mlagFZ2,mlagFZ3))
>________________________________
>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)
>vs.
> sum(mlagFZ1,mlagFZ2,mlagFZ3)/3
>
>
>> mean(mlagFZ1,mlagFZ2,mlagFZ3)
>[1] -0.3326792
>
>>  sum(mlagFZ1,mlagFZ2,mlagFZ3)/3
>[1] -0.201942
>
>R code:
>print(mlagFZ1)
>print(mlagFZ2)
>print(mlagFZ3)
> sum(mlagFZ1,mlagFZ2,mlagFZ3)/3
>mean(mlagFZ1,mlagFZ2,mlagFZ3)
>
>output;
>> 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
>https://nam10.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Crmh%40temple.edu%7Cad57db1be49d481a7b8b08da344e1ce5%7C716e81efb52244738e3110bd02ccf6e5%7C0%7C0%7C637879807634239778%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C3000%7C%7C%7C&sdata=hFFO1S%2BKjqyVjjkiF%2BrI2WoqyYtbAL1eQNRCLWNZrGc%3D&reserved=0<https://nam11.safelinks.protection.outlook.com/?url=https%3A%2F%2Fstat.ethz.ch%2Fmailman%2Flistinfo%2Fr-help&data=05%7C01%7Cjsorkin%40som.umaryland.edu%7Cdf1463e5f1f04d96525408da34510361%7C717009a620de461a88940312a395cac9%7C0%7C0%7C637879819908301628%7CUnknown%7CTWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0%3D%7C2000%7C%7C%7C&sdata=r3GGh3lTBrAXuhmDEbLFYlzYfGWM3gn7iXskUw72B40%3D&reserved=0>
>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.
>
>______________________________________________
>R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>https://stat.ethz.ch/mailman/listinfo/r-help
>PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>and provide commented, minimal, self-contained, reproducible code.

-- 
Sent from my phone. Please excuse my brevity.



More information about the R-help mailing list