From m@rc_@chw@rtz @end|ng |rom me@com  Thu Jan  9 14:18:39 2020
From: m@rc_@chw@rtz @end|ng |rom me@com (Marc Schwartz)
Date: Thu, 9 Jan 2020 08:18:39 -0500
Subject: [Rd] mean
In-Reply-To: <FEF25E56AFC7064784FA97C576467E0A020580ACD1@pdexchbalwst01.ad.insee.intra>
References: <FEF25E56AFC7064784FA97C576467E0A020580ACD1@pdexchbalwst01.ad.insee.intra>
Message-ID: <8A0E38C4-A2EA-4342-930B-503ECFE00D59@me.com>


> On Jan 9, 2020, at 7:40 AM, Lipatz Jean-Luc <jean-luc.lipatz at insee.fr> wrote:
> 
> Hello,
> 
> Is there a reason for the following behaviour?
>> mean(c("1","2","3"))
> [1] NA
> Warning message:
> In mean.default(c("1", "2", "3")) :
>  l'argument n'est ni num?rique, ni logique : renvoi de NA
> 
> But:
>> var(c("1","2","3"))
> [1] 1
> 
> And also:
>> median(c("1","2","3"))
> [1] "2"
> 
> But:
>> quantile(c("1","2","3"),p=.5)
> Error in (1 - h) * qs[i] : 
>  argument non num?rique pour un op?rateur binaire
> 
> It sounds like a lack of symetry. 
> Best regards.
> 
> 
> Jean-Luc LIPATZ
> Insee - Direction g?n?rale
> Responsable de la coordination sur le d?veloppement de R et la mise en oeuvre d'alternatives ? SAS


Hi,

It would appear, whether by design or just inconsistent implementations, perhaps by different authors over time, that the checks for whether or not the input vector is numeric differ across the functions.

A further inconsistency is for median(), where:

> median(c("1", "2", "3", "4"))
[1] NA
Warning message:
In mean.default(sort(x, partial = half + 0L:1L)[half + 0L:1L]) :
  argument is not numeric or logical: returning NA

as a result of there being 4 elements, rather than 3, and the internal checks in the code, where in the case of the input vector having an even number of elements, mean() is used:

    if (n%%2L == 1L) 
        sort(x, partial = half)[half]
    else mean(sort(x, partial = half + 0L:1L)[half + 0L:1L])


Similarly:

> median(factor(c("1", "2", "3")))
Error in median.default(factor(c("1", "2", "3"))) : need numeric data

because the input vector is a factor, rather than character, and the initial check has:

  if (is.factor(x) || is.data.frame(x)) 
          stop("need numeric data")


Regards,

Marc Schwartz



[Rd] mean
Marc Schwartz