[R] ifelse()

David Winsemius dwinsemius at comcast.net
Tue Feb 10 23:10:33 CET 2009


It is checking the series of values of X and returning a series of 1  
and 0's;
    1 if it meets your condition (X>0) and
    0 if it doesn't.

What did you expect?

A more complex invocation might look like this:
 > ifelse( X > 0, 1:3, -1:-2)
[1]  1  2  3  1 -1 -2
Note the recycling of the elements of the yes and no arguments

Consider also:

 > (X>0)+0
[1] 1 1 1 1 0 0


-- 
David Winsemius
On Feb 10, 2009, at 4:44 PM, kayj wrote:

>
> I have a problem with ifelse(), I do not understand how it works.
>
>> X<-c(2,2,1,1,0,0)
>> str(X)
> num [1:6] 2 2 1 1 0 0
>> Y<-ifelse(X>0,1,0)
>> Y
> [1] 1 1 1 1 0 0
>>
>
> Can some one explain what is going on, I do not understand what  
> ifelse is
> doing in this case. Can someone explain the output Y.
>
> Thanks
>
> -- 
> View this message in context: http://www.nabble.com/ifelse%28%29-tp21943309p21943309.html
> Sent from the R help mailing list archive at Nabble.com.
>
> ______________________________________________
> 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