Re: R-alpha: ifelse() -- use '&' , not '&&' !

Martin Maechler (maechler@stat.math.ethz.ch)
Fri, 15 Nov 96 10:13:45 +0100


Date: Fri, 15 Nov 96 10:13:45 +0100
Message-Id: <9611150913.AA18906@>
From: Martin Maechler <maechler@stat.math.ethz.ch>
To: jlindsey@luc.ac.be
In-Reply-To: <9611150806.AA12434@alpha.luc.ac.be> (message from Jim Lindsey on
Subject: Re: R-alpha: ifelse() -- use  '&' , not '&&' !


>>>>> "Jim" == Jim Lindsey <jlindsey@luc.ac.be> writes:

    Jim> Suppose that y is a vector containing non-negative (integral)
    Jim> values.  Why does ifelse(y>0,1,0) return a vector while
    Jim> ifelse(y>0&&dist="Poisson",1,0) returns a scalar?  Jim

Because you should use  '&'  and not  '&&' :

     && :  short-cut operating on scalars only

	A && B   ==>  B is NOT evaluated if A is not TRUE

	A & B	   what you want

> y <- sort(round(rnorm(12),1))
> y
 [1] -1.8 -1.4 -1.1 -0.8 -0.1  0.0  0.2  0.5  1.1  1.2  1.9  2.5
> ifelse(y>0,1,0)
 [1] 0 0 0 0 0 0 1 1 1 1 1 1

> ifelse(y>0 & y<2,1,0)
 [1] 0 0 0 0 0 0 1 1 1 1 1 0

> ifelse(y>0&&y<2,1,0)
[1] 0
> 
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-
r-testers mailing list -- To (un)subscribe, send
subscribe	or	unsubscribe
(in the "body", not the subject !)  To: r-testers-request@stat.math.ethz.ch
=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-