[R] equivalent of ifelse

David Winsemius dwinsemius at comcast.net
Wed Dec 9 18:53:19 CET 2009


On Dec 9, 2009, at 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?
>
> x = c(1,2,3)
> y = c(4,5,6,7)
> z = 3
>
> ifelse(z <= 3,x,y)
>
> would return x and not 1

I worry that this is too simple, so wonder if you have expressed your  
intent clearly.

 > if(z <= 3) {x} else {y}
[1] 1 2 3
>


David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list