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

David K Stevens d@v|d@@teven@ @end|ng |rom u@u@edu
Thu May 12 21:44:22 CEST 2022

John - for both mean and sum, the numbers to be operated on need to be 
in a vector. This can be done with the 'c(...)' function, which takes 
the arguments (your three numbers) and concatenates them into a vector.

 > mean(c(mlagFZ1,mlagFZ2,mlagFZ3))
[1] 0.01984417

 > sum(c(mlagFZ1,mlagFZ2,mlagFZ3))/3
[1] 0.01984417

What you have done is provide the mean (and sum) functions with three 
arguments. If you look at the documentation for 'mean', you have mlagFZ1 
as the 'x' argument, mlagFZ2 as the 'trim' argument, and mlagFZ3 as the 
'na.rm' argument, and makes no sense. With slightly different details, 
the same applies to the 'sum' function.

mean {base}    R Documentation
Arithmetic Mean
Generic function for the (trimmed) arithmetic mean.

mean(x, ...)

## Default S3 method:
mean(x, trim = 0, na.rm = FALSE, ...)
An R object. Currently there are methods for numeric/logical vectors and 
date, date-time and time interval objects. Complex vectors are allowed 
for trim = 0, only.

the fraction (0 to 0.5) of observations to be trimmed from each end of x 
before the mean is computed. Values of trim outside that range are taken 
as the nearest endpoint.

a logical value indicating whether NA values should be stripped before 
the computation proceeds.
On 5/12/2022 1:31 PM, Sorkin, John wrote:
> 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://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> CAUTION: This email originated from outside of USU. If this appears to be a USU employee, beware of impersonators. Do not click links, reply, download images, or open attachments unless you verify the sender’s identity and know the content is safe.
David K Stevens, PhD,PE
Civil and Environmental Engineering
Utah State University
Logan, UT 84322-8200
david.stevens using usu.edu

	[[alternative HTML version deleted]]

More information about the R-help mailing list