[R] odd feature

Gabor Grothendieck ggrothendieck at gmail.com
Sun May 21 15:47:07 CEST 2006


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
>



More information about the R-help mailing list