[R] why the a[-indx] does not work?

S Ellison S.Ellison at LGCGroup.com
Wed Nov 2 12:33:36 CET 2011


> -----Original Message-----
> From:Alaios
> Sent: 30 October 2011 21:09
> To: William Dunlap; andrija djurovic
> Cc: R-help at r-project.org
> Subject: Re: [R] why the a[-indx] does not work?
> What is the difference between though
> !numericVector==0 and
> -numericVector==0
Er... you need to be (a lot) more careful with operator precendence. See ?Syntax for operator precedence.

will usually* give the same answer as 

because unary minus has higher precedence than ==, so this is read implicitly as (-numericvector)==0. -1 and 1 are still both nozero, while -0 and 0 are both still zero. ( *'usually' because you may be comparing a double precision nearly-zero with another double precision nearly-zero, and that is _always_ asking for trouble.)

behaves quite differently because unary negation (!, or NOT) has _lower_ precedence than ==, so this one is read as

Operatopr preference rules for programmers:
Rule 1: If in doubt about operator precedence, use parentheses
Rule 2: Always have doubts about operator precedence unless you have looked it up for _that_ version of _that_  language _that day_.
Rule 3: Check the operator precedence of parentheses.

S Ellison

This email and any attachments are confidential. Any use...{{dropped:8}}

More information about the R-help mailing list