[R] why sd() can be applied to character vector?
R. Michael Weylandt
michael.weylandt at gmail.com
Sat Oct 27 11:48:08 CEST 2012
On Sat, Oct 27, 2012 at 10:16 AM, Jinsong Zhao <jszhao at yeah.net> wrote:
> Hi there,
> In the following example, sd() can be applied to a character vector.
> However, mean() can not be run in a similar way. Why?
> I have read sd() man page, however, I don't find information about that
>> x <- as.character(1:10)
>  3.02765
>  NA
> Warning message:
> In mean.default(x) : argument is not numeric or logical: returning NA
Practically, I see
x = PROTECT(coerceVector(x, REALSXP));
which seems to be responsible for the behavior you note (eventually
calling $R_HOME/src/main/util.c:1463), but mean.default catches the
character case early and returns NA_real_.
None of that explains the reasoning however and it does seem a little anomalous
More information about the R-help