# character handling: mean() vs sd()

[This article was first published on

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

**Odd Hypothesis**, and kindly contributed to R-bloggers]. (You can report issue about the content on this page here)Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.

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)`

[1] NA

# sd(x)

[1] 1

So, `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.

To

**leave a comment**for the author, please follow the link and comment on their blog:**Odd Hypothesis**.R-bloggers.com offers

**daily e-mail updates**about R news and tutorials about learning R and many other topics. Click here if you're looking to post or find an R/data-science job.

Want to share your content on R-bloggers? click here if you have a blog, or here if you don't.