Sorkin, John
2022-May-12 19:31 UTC
[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?
Ivan Krylov
2022-May-12 19:41 UTC
[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
David K Stevens
2022-May-12 19:44 UTC
[R] [EXT] result of mean(v1, v2, v3) of three real number not the same as sum(v1, v2, v3)/3
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 Description Generic function for the (trimmed) arithmetic mean. Usage mean(x, ...) ## Default S3 method: mean(x, trim = 0, na.rm = FALSE, ...) Arguments x 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. trim 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. na.rm 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see > 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 Professor Civil and Environmental Engineering Utah State University Logan, UT 84322-8200 david.stevens at usu.edu 014357973229 [[alternative HTML version deleted]]
Marc Schwartz
2022-May-12 19:47 UTC
[R] result of mean(v1, v2, v3) of three real number not the same as sum(v1, v2, v3)/3
Hi John, sum() takes an arbitrary "..." list as the initial argument, while mean() takes a vector "x" as the initial named argument. Thus:> sum(-0.3326792, -0.1890601, -0.0840866) / 3[1] -0.201942> mean(c(-0.3326792, -0.1890601, -0.0840866))[1] -0.201942 Regards, Marc Schwartz On May 12, 2022 at 3:31:51 PM, Sorkin, John (jsorkin at som.umaryland.edu (mailto:jsorkin at som.umaryland.edu)) 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?
Richard M. Heiberger
2022-May-12 19:52 UTC
[R] [External] 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 at r-project.org> on behalf of Sorkin, John <jsorkin at som.umaryland.edu> Sent: Thursday, May 12, 2022 3:31:51 PM To: r-help at r-project.org <r-help at 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 at r-project.org mailing list -- To UNSUBSCRIBE and more, see nam10.safelinks.protection.outlook.com/?url=https://stat.ethz.ch/mailman/listinfo/r-help&data=05|01|rmh@temple.edu|ad57db1be49d481a7b8b08da344e1ce5|716e81efb52244738e3110bd02ccf6e5|0|0|637879807634239778|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|3000|||&sdata=hFFO1S+KjqyVjjkiF+rI2WoqyYtbAL1eQNRCLWNZrGc=&reserved=0 PLEASE do read the posting guide nam10.safelinks.protection.outlook.com/?url=http://www.r-project.org/posting-guide.html&data=05|01|rmh@temple.edu|ad57db1be49d481a7b8b08da344e1ce5|716e81efb52244738e3110bd02ccf6e5|0|0|637879807634239778|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|3000|||&sdata=h7unlucyqKJ2yWZ2nqVhZWekHPRq/beTBEy1OSKIfZ0=&reserved=0 and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]]