character handling: mean() vs sd()

[This article was first published on 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?

<span class="pln">x </span><span class="pun">=</span><span class="pln"> c</span><span class="pun">(</span><span class="str">'1'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'2'</span><span class="pun">,</span><span class="pln"> </span><span class="str">'3'</span><span class="pun">)</span><span class="pln"><br />mean</span><span class="pun">(</span><span class="pln">x</span><span class="pun">)</span><span class="pln"><br />sd</span><span class="pun">(</span><span class="pln">x</span><span class="pun">)</span>

Well, apparently it is:

<span class="com"># mean(x)</span><span class="pln"><br /></span><span class="pun">[</span><span class="lit">1</span><span class="pun">]</span><span class="pln"> NA<br /><br /></span><span class="com"># sd(x)</span><span class="pln"><br /></span><span class="pun">[</span><span class="lit">1</span><span class="pun">]</span><span class="pln"> </span><span class="lit">1</span>

So, sd() silently converts its input to numeric, while mean() does not. More evidence of this is in the source:

<span class="pun">></span><span class="pln"> mean<br /></span><span class="kwd">function</span><span class="pln"> </span><span class="pun">(</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> </span><span class="pun">...)</span><span class="pln"> <br /></span><span class="typ">UseMethod</span><span class="pun">(</span><span class="str">"mean"</span><span class="pun">)</span><span class="pln"><br /></span><span class="pun"><</span><span class="pln">bytecode</span><span class="pun">:</span><span class="pln"> </span><span class="lit">0x000000001165e790</span><span class="pun">></span><span class="pln"><br /></span><span class="pun"><</span><span class="pln">environment</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">namespace</span><span class="pun">:</span><span class="kwd">base</span><span class="pun">></span><span class="pln"><br /><br /></span><span class="pun">></span><span class="pln"> sd<br /></span><span class="kwd">function</span><span class="pln"> </span><span class="pun">(</span><span class="pln">x</span><span class="pun">,</span><span class="pln"> na</span><span class="pun">.</span><span class="pln">rm </span><span class="pun">=</span><span class="pln"> FALSE</span><span class="pun">)</span><span class="pln"> <br />sqrt</span><span class="pun">(</span><span class="kwd">var</span><span class="pun">(</span><span class="kwd">if</span><span class="pln"> </span><span class="pun">(</span><span class="kwd">is</span><span class="pun">.</span><span class="pln">vector</span><span class="pun">(</span><span class="pln">x</span><span class="pun">))</span><span class="pln"> x </span><span class="kwd">else</span><span class="pln"> </span><span class="kwd">as</span><span class="pun">.</span><span class="kwd">double</span><span class="pun">(</span><span class="pln">x</span><span class="pun">),</span><span class="pln"> na</span><span class="pun">.</span><span class="pln">rm </span><span class="pun">=</span><span class="pln"> na</span><span class="pun">.</span><span class="pln">rm</span><span class="pun">))</span><span class="pln"><br /></span><span class="pun"><</span><span class="pln">bytecode</span><span class="pun">:</span><span class="pln"> </span><span class="lit">0x000000001158eb00</span><span class="pun">></span><span class="pln"><br /></span><span class="pun"><</span><span class="pln">environment</span><span class="pun">:</span><span class="pln"> </span><span class="kwd">namespace</span><span class="pun">:</span><span class="pln">stats</span><span class="pun">></span>

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.

Never miss an update!
Subscribe to R-bloggers to receive
e-mails with the latest R posts.
(You will not see this message again.)

Click here to close (This popup will not appear again)