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

Ivan Krylov kry|ov@r00t @end|ng |rom gm@||@com
Thu May 12 22:05:14 CEST 2022


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



More information about the R-help mailing list