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]]
Sorkin, John
2022-May-12 20:55 UTC
[R] [External] result of mean(v1, v2, v3) of three real number not the same as sum(v1, v2, v3)/3
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 at temple.edu> Sent: Thursday, May 12, 2022 3:52 PM To: Sorkin, John; r-help at 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 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<nam11.safelinks.protection.outlook.com/?url=https://stat.ethz.ch/mailman/listinfo/r-help&data=05|01|jsorkin@som.umaryland.edu|df1463e5f1f04d96525408da34510361|717009a620de461a88940312a395cac9|0|0|637879819908301628|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000|||&sdata=r3GGh3lTBrAXuhmDEbLFYlzYfGWM3gn7iXskUw72B40=&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<nam11.safelinks.protection.outlook.com/?url=http://www.r-project.org/posting-guide.html&data=05|01|jsorkin@som.umaryland.edu|df1463e5f1f04d96525408da34510361|717009a620de461a88940312a395cac9|0|0|637879819908301628|Unknown|TWFpbGZsb3d8eyJWIjoiMC4wLjAwMDAiLCJQIjoiV2luMzIiLCJBTiI6Ik1haWwiLCJXVCI6Mn0=|2000|||&sdata=0FD7GgnSphuw1Ha419vFHTDw3aGPIjXrwPigrMkMfOU=&reserved=0> and provide commented, minimal, self-contained, reproducible code.