Dear R wizards: I just got stung by the ifelse() feature.> a <- 10:15 > b <- 20:300 > test <- 1 > ifelse(test,a,b)[1] 10 I had not realized that this was the default behavior---I had expected 10:15. mea culpa. however, I wonder whether it would make sense to replace ifelse with a different semantic, where if test is a single scalar, it means what a stupid user like me would imagine. Aside, I like the flexibility of R, but I am not thrilled by all the recycling rules. I either mean I want a scalar or a vector of equal/appropriate dimension. I never want a recycle of a smaller vector. (I do often use a recycle of a scalar.) regards, /iaw
If you know that test is a scalar result <- if (test) a else b will do it. Here is another approach: as.vector(test * ts(a) + (!test) * ts(b)) On 5/21/06, ivo welch <ivowel at gmail.com> wrote:> Dear R wizards: > > I just got stung by the ifelse() feature. > > > a <- 10:15 > > b <- 20:300 > > test <- 1 > > ifelse(test,a,b) > [1] 10 > > I had not realized that this was the default behavior---I had expected > 10:15. mea culpa. however, I wonder whether it would make sense to > replace ifelse with a different semantic, where if test is a single > scalar, it means what a stupid user like me would imagine. > > Aside, I like the flexibility of R, but I am not thrilled by all the > recycling rules. I either mean I want a scalar or a vector of > equal/appropriate dimension. I never want a recycle of a smaller > vector. (I do often use a recycle of a scalar.) > > regards, > > /iaw > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >
ivo welch wrote:> Dear R wizards: > > I just got stung by the ifelse() feature. > > >>a <- 10:15 >>b <- 20:300 >>test <- 1 >>ifelse(test,a,b) > > [1] 10 > > I had not realized that this was the default behavior---I had expected > 10:15. mea culpa. however, I wonder whether it would make sense to > replace ifelse with a different semantic, where if test is a single > scalar, it means what a stupid user like me would imagine. > > Aside, I like the flexibility of R, but I am not thrilled by all the > recycling rules. I either mean I want a scalar or a vector of > equal/appropriate dimension. I never want a recycle of a smaller > vector. (I do often use a recycle of a scalar.) > > regards, > > /iawThe current behavior is logical to me. Are you looking for if(test) a else b? Frank -- Frank E Harrell Jr Professor and Chair School of Medicine Department of Biostatistics Vanderbilt University
>>>>> "Gabor" == Gabor Grothendieck <ggrothendieck at gmail.com> >>>>> on Sun, 21 May 2006 09:47:07 -0400 writes:Gabor> If you know that test is a scalar Gabor> result <- if (test) a else b Gabor> will do it. Yes, indeed! IMO, ifelse(test, a, b) is much overused where as if(test) a else b is much UNDER used.>From some e-mail postings, and even some documents (even printedbooks?), I get the impression that too many people think that ifelse(.,.,.) is to be used as expression / function and if(.) . else . only for "program flow control". This leads to quite suboptimal code, and I personally use if(.) . else . __as expression__ much more frequently than ifelse(.,.,.) Martin Maechler, ETH Zurich. Gabor> Here is another approach: Gabor> as.vector(test * ts(a) + (!test) * ts(b)) Gabor> On 5/21/06, ivo welch <ivowel at gmail.com> wrote: >> Dear R wizards: >> >> I just got stung by the ifelse() feature. >> >> > a <- 10:15 >> > b <- 20:300 >> > test <- 1 >> > ifelse(test,a,b) >> [1] 10 >> >> I had not realized that this was the default behavior---I had expected >> 10:15. mea culpa. however, I wonder whether it would make sense to >> replace ifelse with a different semantic, where if test is a single >> scalar, it means what a stupid user like me would imagine. >> >> Aside, I like the flexibility of R, but I am not thrilled by all the >> recycling rules. I either mean I want a scalar or a vector of >> equal/appropriate dimension. I never want a recycle of a smaller >> vector. (I do often use a recycle of a scalar.) >> >> regards, >> >> /iaw >> >> ______________________________________________ >> R-help at stat.math.ethz.ch mailing list >> https://stat.ethz.ch/mailman/listinfo/r-help >> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html >> Gabor> ______________________________________________ Gabor> R-help at stat.math.ethz.ch mailing list Gabor> https://stat.ethz.ch/mailman/listinfo/r-help Gabor> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
On Sun, 21 May 2006, ivo welch wrote:> > Aside, I like the flexibility of R, but I am not thrilled by all the > recycling rules. I either mean I want a scalar or a vector of > equal/appropriate dimension. I never want a recycle of a smaller > vector. (I do often use a recycle of a scalar.) >One case where the vector-vector recycling rules are used is in vector-matrix operations: a<-1:4 b<-diag(4) a+b -thomas
Hi Thomas:> One case where the vector-vector recycling rules are used is in > vector-matrix operations: > a<-1:4 > b<-diag(4) > a+bis this last expression intended to be intuitive and thus desirable? if anything else, I would end up writing something like this more as an error than by intent. I would suggest that recycling might be better to be explicitly asked for by the user---though I am not sure what the syntax should be. allow.recycle(a+b) at the end, I think implicit recycling is a tradeoff between convenience and unintended errors. I would judge the convenience of implicit recycling to be fairly low, and the unintended error rate [with difficulty finding it] to be fairly high. I guess the R language has few options() that control its behavior---e.g., ala "use strict;" in perl. If it did, I would love to turn off implicit recycling, provided there is an explicit recycle possibility. I would not mind having the ability to be forced to define variables first, either, though this is not a big deal. R is pretty good in telling me when I use variables that have not been defined. regards, /ivo> > > -thomas >