Here’s a weird R error/bug/nuance I came across today:
What would you expect the following lines of code to return?
x = c('1', '2', '3') mean(x) sd(x)
Well, apparently it is:
# mean(x)  NA # sd(x)  1
sd() silently converts its input to numeric, while
mean() does not. More evidence of this is in the source:
> mean function (x, ...) UseMethod("mean") <bytecode: 0x000000001165e790> <environment: namespace:base> > sd function (x, na.rm = FALSE) sqrt(var(if (is.vector(x)) x else as.double(x), na.rm = na.rm)) <bytecode: 0x000000001158eb00> <environment: namespace:stats>
One hour of my work day was spent sorting this out. You’ve been warned.
Written with StackEdit.