[R] equivalent of ifelse

Duncan Murdoch murdoch at stats.uwo.ca
Wed Dec 9 18:56:46 CET 2009


On 09/12/2009 12:40 PM, carol white wrote:
> Hi,
> Is there any equivalent for ifelse (except if (cond) expr1 else expr2) which takes an atomic element as argument but returns vector since ifelse returns an object of the same length as its argument?
>   

I don't understand what's wrong with  "if (cond) expr1 else expr2".  It 
can be used in an expression, e.g.

w <- if (z <= 3) x else y

which is I think exactly what you are asking for.

Duncan Murdoch
> x = c(1,2,3)
> y = c(4,5,6,7)
> z = 3
>
> ifelse(z <= 3,x,y)
>
> would return x and not 1
>   
> thanks
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>




More information about the R-help mailing list