As far as mean
is concerned it is quite straightforward. As @Rich Scriven mentions if you type mean.default
in the console you see a section of code
if (na.rm)
x <- x[!is.na(x)]
which gives you the error.
mean(1:10, na.rm = "abc") #gives
Error in if (na.rm) x <- x[!is.na(x)] :
argument is not interpretable as logical
which is similar to doing
if ("abc") "Hello"
Error in if ("abc") "Hello" : argument is not interpretable as logical
Now regarding sum
, min
, max
and other primitive functions which is implemented in C. The source code of these functions is here. There is a parameter Rboolean narm
passed into the function.
The way C treats boolean is different.
#include <stdio.h>
#include <stdbool.h>
int main()
{
bool a = "abc";
if (a)
printf("Hello World");
else
printf("Not Hello World");
return 0;
}
If you run the above C
code it will print "Hello World". Run the demo here. If you pass a string input to boolean type it is considered as TRUE
in C
. In fact that is even true with numbers as well
sum(1:10, na.rm = 12)
works as well.
PS - I am no expert in C and know a little bit of R. Finding all these insights took lot of time. Let me know if I have misinterpreted something and provided any false information.
min/max/sum
are primitives whilemean
is not. The processing ofif (na.rm)
produces an error inmean.default
, and I assume it does not inmin/max/sum
due to their being primitives. – Respectivena.rm
would be evaluated & coerced consistently across the board. Note thatna.rm="FALSE"
is indeed parsed as a logical, so it's not that any string becomes TRUE, cf.sum(c(1:3,NA), na.rm="xyz") == 6
,sum(c(1:3,NA), na.rm="TRUE") == 6
, andsum(c(1:3,NA), na.rm="FALSE") == NA
. – Fluidics