Skip to content

ifelse()

6 messages · kayj, Sarah Goslee, Seeliger.Curt at epamail.epa.gov +3 more

#
I have a problem with ifelse(), I do not understand how it works.
num [1:6] 2 2 1 1 0 0
[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
#
Did you read the helpfile?

If your condition is true, the first option is returned. If it is false, the
second option is returned.

For the first four elements of X, all of which are greater than zero,
1 is returned. For the last two, which are not greater than zero,
0 is returned.

Sarah
On Tue, Feb 10, 2009 at 4:44 PM, kayj <kjaja27 at yahoo.com> wrote:

  
    
#
ifelse goes through your vector X, 1 element at a time, and if the element is greater than zero it returns a 1, otherwise, 0.  The resulting vector of 1s and 0s is assigned to Y

Hope this is helpful,

Dan

Daniel J. Nordlund
Washington State Department of Social and Health Services
Planning, Performance, and Accountability
Research and Data Analysis Division
Olympia, WA  98504-5204
#
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:
[1]  TRUE  TRUE  TRUE  TRUE FALSE FALSE
[1] 1 1 1 1 0 0
Now the second and third parameters can also be vectors and the
corresponding value will be chosen:
[1]  1  2  3  4 15 16
HTH
On Tue, Feb 10, 2009 at 4:44 PM, kayj <kjaja27 at yahoo.com> wrote: