# [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
>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