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
Mike Miller <mbmiller+l <at> gmail.com> writes:> > 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 is indeed documented, in passing, in one of the pages you listed: http://tim-smith.us/arrgh/syntax.html Whitespace is meaningless, unless it isn't. Some parsing ambiguities are resolved by considering whitespace around operators. See and despair: x<-y (assignment) is parsed differently than x < -y (comparison)!
All the more reason to use = instead of <- -----Original Message----- From: R-help [mailto:r-help-bounces at r-project.org] On Behalf Of Ben Bolker Sent: Monday, 2 February 2015 2:07p To: r-help at stat.math.ethz.ch Subject: Re: [R] the less-than-minus gotcha Mike Miller <mbmiller+l <at> gmail.com> writes:> > 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 is indeed documented, in passing, in one of the pages you listed: http://tim-smith.us/arrgh/syntax.html Whitespace is meaningless, unless it isn't. Some parsing ambiguities are resolved by considering whitespace around operators. See and despair: x<-y (assignment) is parsed differently than x < -y (comparison)! ______________________________________________ R-help at 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.