Sorkin, John
2022-May-12 19:45 UTC
[R] result of mean(v1, v2, v3) of three real number not the same as sum(v1, v2, v3)/3
Ivan, Thank you for your quick reply. Unfortunately, I don't understand the concept you are trying toe explain. Can you try again? Thank you, John ________________________________________ From: Ivan Krylov <krylov.r00t at gmail.com> Sent: Thursday, May 12, 2022 3:41 PM To: Sorkin, John Cc: r-help at r-project.org Subject: Re: [R] result of mean(v1, v2, v3) of three real number not the same as sum(v1, v2, v3)/3 On Thu, 12 May 2022 19:31:51 +0000 "Sorkin, John" <jsorkin at som.umaryland.edu> wrote:> > mean(mlagFZ1,mlagFZ2,mlagFZ3) > [1] -0.3326792match.call(mean.default, quote(mean(mlagFZ1, mlagFZ2, mlagFZ3))) # mean(x = mlagFZ1, trim = mlagFZ2, na.rm = mlagFZ3) mean() takes a vector to compute a mean of and additional arguments, unlike sum(), which takes ... (almost arbitrary arguments) and sums all of them. Unfortunately, the "trim" argument is documented to accept any number and the na.rm argument is silently reinterpreted as logical in your case. -- Best regards, Ivan
Ivan Krylov
2022-May-12 20:05 UTC
[R] result of mean(v1, v2, v3) of three real number not the same as sum(v1, v2, v3)/3
Eric Berger and Marc Schwartz and David K Stevens probably said it better. I was trying to illustrate the way mean() takes its arguments using the match.call function. The sum() function can take individual numbers or vectors and sum all their elements, so sum(c(1, 2, 3)) is the same as sum(1, 2, 3), or even sum(c(1, 2), 3): they all do what you mean them to do. The mean() function is different. It may accept many arguments, but only the first of them is the vector of numbers you're interested in: mean(c(1, 2, 3)) is the correct way to call it. Unfortunately, when you give it more arguments and they aren't what mean() expects them to be (the second one should be a number in [0; 0.5] and the third one should be TRUE or FALSE, see help(mean) if you're curious), R doesn't warn you or raise an error condition. My use of match.call() was supposed to show that by calling mean(a, b, c), I pass the number "b" as the "trim" argument to mean() and the number "c" as the "na.rm" argument to mean(), which is not what was intended here. -- Best regards, Ivan