From maechler at stat.math.ethz.ch  Mon Nov 28 10:48:39 2016
From: maechler at stat.math.ethz.ch (Martin Maechler)
Date: Mon, 28 Nov 2016 10:48:39 +0100
Subject: [Rd] ifelse() woes ... can we agree on a ifelse2() ?
In-Reply-To: <879999629.947686.1480213634160@mail.yahoo.com>
References: <879999629.947686.1480213634160.ref@mail.yahoo.com>
	<879999629.947686.1480213634160@mail.yahoo.com>
Message-ID: <22587.64887.844012.761038@stat.math.ethz.ch>


> Related to the length of 'ifelse' result, I want to say that "example of different return modes" in ?ifelse led me to perceive a wrong thing in the past.
>      ## example of different return modes:
>      yes <- 1:3
>      no <- pi^(0:3)
>      typeof(ifelse(NA,    yes, no)) # logical
>      typeof(ifelse(TRUE,  yes, no)) # integer
>      typeof(ifelse(FALSE, yes, no)) # double
> 
> As the result of each 'ifelse' call is not printed, I thought that the length of the result is 3. In fact, the length of the result is 1.

"of course"... (;-)

But this indeed proves that the example is too sophisticated and
not helpful/clear enough.
Is this better?

## example of different return modes (and 'test' alone determining length):
yes <- 1:3
no  <- pi^(1:4)
utils::str( ifelse(NA,    yes, no) ) # logical, length 1
utils::str( ifelse(TRUE,  yes, no) ) # integer, length 1
utils::str( ifelse(FALSE, yes, no) ) # double,  length 1



> I realize just now that the length of 'no' is different from 'yes'. The length of 'yes' is 3, the length of 'no' is 4.



[Rd] ifelse() woes ... can we agree on a ifelse2() ?
Martin Maechler