[R] Mean error message missing
Dear list, I found an odd behavior of the mean function; it is allowed to do something that you probably shouldn't: If you calculate mean() of a sequence of numbers (without declaring them as vector), mean() then just computes mean() of the first element. Is there a reason why there is no warning, like in sd for example? Example code: mean(1,2,3,4) sd(1,2,3,4) Best regards Christian __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Mean error message missing
On Mon, 8 Jun 2015, Christian Brandstätter wrote: Dear list, I found an odd behavior of the mean function; it is allowed to do something that you probably shouldn't: If you calculate mean() of a sequence of numbers (without declaring them as vector), mean() then just computes mean() of the first element. Is there a reason why there is no warning, like in sd for example? mean() - unlike sd() - is a generic function that has a '...' argument that is passed on to its methods. The default method which is called in your example also has a '...' argument (because the generic has it) but doesn't use it. Example code: mean(1,2,3,4) sd(1,2,3,4) Best regards Christian __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Mean error message missing
Thank you for the explanation. But if you take for instance plot.default(), being another generic function, it would not work like that: plot(1,2,3,4), only plot(1,2) is accepted. From R-help (Usage): ## Default S3 method: mean(x, trim = 0, na.rm = FALSE, ...) What is puzzling, is that apparently na.rm (and trim, which is indicated in the help) is accepting numeric values. mean(c(1,NA,10),10,TRUE) mean(c(1,NA,10),10,FALSE) This should give at least a warning in my opinion. mean(c(1,NA,10),10,200) On 08/06/2015 09:27, Achim Zeileis wrote: On Mon, 8 Jun 2015, Christian Brandst�tter wrote: Dear list, I found an odd behavior of the mean function; it is allowed to do something that you probably shouldn't: If you calculate mean() of a sequence of numbers (without declaring them as vector), mean() then just computes mean() of the first element. Is there a reason why there is no warning, like in sd for example? mean() - unlike sd() - is a generic function that has a '...' argument that is passed on to its methods. The default method which is called in your example also has a '...' argument (because the generic has it) but doesn't use it. Example code: mean(1,2,3,4) sd(1,2,3,4) Best regards Christian __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Mean error message missing
On 08/06/2015 6:04 AM, Christian Brandstätter wrote: Thank you for the explanation. But if you take for instance plot.default(), being another generic function, it would not work like that: plot(1,2,3,4), only plot(1,2) is accepted. From R-help (Usage): ## Default S3 method: mean(x, trim = 0, na.rm = FALSE, ...) What is puzzling, is that apparently na.rm (and trim, which is indicated in the help) is accepting numeric values. mean(c(1,NA,10),10,TRUE) mean(c(1,NA,10),10,FALSE) This should give at least a warning in my opinion. It is a common idiom in R programming to treat non-zero values as TRUE, and zero as FALSE. If every use of a number where a logical is needed generated a warning, you'd be swamped with them. Duncan Murdoch mean(c(1,NA,10),10,200) On 08/06/2015 09:27, Achim Zeileis wrote: On Mon, 8 Jun 2015, Christian Brandst�tter wrote: Dear list, I found an odd behavior of the mean function; it is allowed to do something that you probably shouldn't: If you calculate mean() of a sequence of numbers (without declaring them as vector), mean() then just computes mean() of the first element. Is there a reason why there is no warning, like in sd for example? mean() - unlike sd() - is a generic function that has a '...' argument that is passed on to its methods. The default method which is called in your example also has a '...' argument (because the generic has it) but doesn't use it. Example code: mean(1,2,3,4) sd(1,2,3,4) Best regards Christian __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Mean error message missing
Thank you very much, I didn't know that. On 08/06/2015 6:04 AM, Christian Brandstätter wrote: Thank you for the explanation. But if you take for instance plot.default(), being another generic function, it would not work like that: plot(1,2,3,4), only plot(1,2) is accepted. From R-help (Usage): ## Default S3 method: mean(x, trim = 0, na.rm = FALSE, ...) What is puzzling, is that apparently na.rm (and trim, which is indicated in the help) is accepting numeric values. mean(c(1,NA,10),10,TRUE) mean(c(1,NA,10),10,FALSE) This should give at least a warning in my opinion. It is a common idiom in R programming to treat non-zero values as TRUE, and zero as FALSE. If every use of a number where a logical is needed generated a warning, you'd be swamped with them. Duncan Murdoch mean(c(1,NA,10),10,200) On 08/06/2015 09:27, Achim Zeileis wrote: On Mon, 8 Jun 2015, Christian Brandst�tter wrote: Dear list, I found an odd behavior of the mean function; it is allowed to do something that you probably shouldn't: If you calculate mean() of a sequence of numbers (without declaring them as vector), mean() then just computes mean() of the first element. Is there a reason why there is no warning, like in sd for example? mean() - unlike sd() - is a generic function that has a '...' argument that is passed on to its methods. The default method which is called in your example also has a '...' argument (because the generic has it) but doesn't use it. Example code: mean(1,2,3,4) sd(1,2,3,4) Best regards Christian __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Mean error
Hi, Try either: res1 - apply(mydata[,1:2],2,mean) res2 - colMeans(mydata[,1:2]) identical(res1,res2) #[1] TRUE # Also if you need to find means for each group (Ungrazed vs. Grazed) by(mydata[,-3],mydata[,3],colMeans) #or if column names are V1, V2, V3 aggregate(.~V3,mydata,mean) #or library(plyr) ddply(mydata,.(V3),numcolwise(mean)) A.K. I have a data set with two columns of data that I want to find the mean of. 1 6.225 59.77 Ungrazed 2 6.487 60.98 Ungrazed 3 4.919 14.73 Ungrazed 4 5.130 19.28 Ungrazed 5 5.417 34.25 Ungrazed 6 5.359 35.53 Ungrazed 7 7.614 87.73 Ungrazed 8 6.352 63.21 Ungrazed 9 4.975 24.25 Ungrazed 10 6.930 64.34 Ungrazed 11 6.248 52.92 Ungrazed 12 5.451 32.35 Ungrazed 13 6.013 53.61 Ungrazed 14 5.928 54.86 Ungrazed 15 6.264 64.81 Ungrazed 16 7.181 73.24 Ungrazed 17 7.001 80.64 Ungrazed 18 4.426 18.89 Ungrazed 19 7.302 75.49 Ungrazed 20 5.836 46.73 Ungrazed 21 10.253 116.05 Ungrazed 22 6.958 38.94 Grazed 23 8.001 60.77 Grazed 24 9.039 84.37 Grazed 25 8.910 70.11 Grazed 26 6.106 14.95 Grazed 27 7.691 70.70 Grazed 28 8.988 80.31 Grazed 29 8.975 82.35 Grazed 30 9.844 105.07 Grazed 31 8.508 73.79 Grazed 32 7.354 50.08 Grazed 33 8.643 78.28 Grazed 34 7.916 41.48 Grazed 35 9.351 98.47 Grazed 36 7.066 40.15 Grazed 37 8.158 52.26 Grazed 38 7.382 46.64 Grazed 39 8.515 71.01 Grazed 40 8.530 83.03 Grazed This is from an introduction handout that instructs me to enter the command mean(mydata[,1:2]) but when I enter it, I get an error message Warning message: In mean.default(mydata[, 1:2]) : argument is not numeric or logical: returning NA I've tried tacking on na.rm=T to the end of it, but I get the same message. Can someone tell me what I'm doing wrong, or how to fix it? I've tried searching the forum, but can't find a post relevant to this problem. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.