Skip to content

Mean error message missing

5 messages · Achim Zeileis, Christian Brandstätter, Duncan Murdoch

#
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
#
On Mon, 8 Jun 2015, Christian Brandst?tter wrote:

            
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.
#
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 08/06/2015 6:04 AM, Christian Brandst?tter wrote:
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
#
Thank you very much, I didn't know that.