I've got to remember to use more spaces.  Here's the basic problem:

These are the same:

v< 1
v<1

But these are extremely different:

v< -1
v<-1

This mistake can get you even inside of a function call like this:

v <- -2:2
which( v<1 )
[1] 1 2 3
which( v<-1 )  # oops, I meant v< -1 not v<-1 a HUGE mistake!
Error in which(v <- 1) : argument to 'which' is not logical
v
[1] 1

It throws an error but not because I just destroyed my data. R has no way of knowing that I didn't intend to overwrite the vector.

This was how it got me:

which( frame$var>4 )   # no problem

which( frame$var<-4 )  # huge problem: frame$var is destroyed

Too late now: The data in frame$var were all overwritten with 4s. So the data are lost and might not be recoverable.

Maybe the tactic that will save me in the future is to remember to always use plenty of spaces:

which( frame$var > 4 )

which( frame$var < -4 )

That also makes the code easier to read. In a script, I probably would have done that, but in an interactive session I can be lazy.

It seems that there are a lot of "R gotcha" pages on the web but quite a few of the examples show R behaving exactly like I would want and expect (e.g., of course NA + 5 returns NA)...

https://github.com/mikelove/r-gotchas/blob/master/README.md
http://stackoverflow.com/questions/1535021/whats-the-biggest-r-gotcha-youve-run-across
http://www.burns-stat.com/pages/Tutor/R_inferno.pdf
http://biostat.mc.vanderbilt.edu/wiki/Main/LinuxWorkshopRProgramingTipsAndGotchas
http://tim-smith.us/arrgh/

I didn't happen to see my example on any list, but I didn't read them thoroughly, so it's probably there.

Mike

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.

Reply via email to