[R] ifelse()
jim holtman
jholtman at gmail.com
Tue Feb 10 23:10:30 CET 2009
Take it in a couple of steps. 'ifelse' will take the evaluation of a
logical vector (first parameter) and return it second parameter if
TRUE or the third parameter if FALSE:
> X<-c(2,2,1,1,0,0)
> X > 0
[1] TRUE TRUE TRUE TRUE FALSE FALSE
> ifelse(X > 0, 1,0)
[1] 1 1 1 1 0 0
>
Now the second and third parameters can also be vectors and the
corresponding value will be chosen:
> ifelse(X > 0, 1:6, 11:16)
[1] 1 2 3 4 15 16
>
HTH
On Tue, Feb 10, 2009 at 4:44 PM, kayj <kjaja27 at yahoo.com> 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-tp21943308p21943308.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.
>
--
Jim Holtman
Cincinnati, OH
+1 513 646 9390
What is the problem that you are trying to solve?
More information about the R-help
mailing list