R pitfalls #4: redefining the basics

December 14, 2012
By

(This article was originally published at Quantum Forest » rblogs, and syndicated at StatsBlogs.)

I try to be economical when writing code; for example, I tend to use single quotes over double quotes for characters because it saves me one keystroke. One area where I don’t do that is when typing TRUE and FALSE (R accepts T and F as well), just because it is clearer to see in code and syntax highlighting kicks in. That’s why I was surprised to read Jason Morgan’s post in that it is possible to redefine T and F and get undesirable behavior.

Playing around it is quite easy to redefine other fundamental constants in R. For example, I posted in Twitter:

> pi
[1] 3.141593
> pi <- 2
> pi*2
[1] 4

Ouch, dangerous! I tend to muck around with matrices quite a bit and, being a friend of parsimony, I often use capital letters to represent them. This would have eventually bitten me if I had used the abbreviated TRUE and FALSE. As Kevin Ushey replied to my tweet, one can redefine even basic functions like ‘+’ and be pure evil; over the top, sure, but possible.

Clown faces
Some times coding is scary (Photo: Luis).



Please comment on the article here: Quantum Forest » rblogs

Tags: ,


Subscribe

Email:

  Subscribe