Hi. Thanks. Which part? I looked at the below but I don't think it clearly
addresses the nested ifelse situation.


ifelse {base}R DocumentationConditional Element Selection Description

ifelse returns a value with the same shape as test which is filled with
elements selected from either yes or no depending on whether the element of
test is TRUE or FALSE.
Usage

ifelse(test, yes, no)

Argumentstest

an object which can be coerced to logical mode.
yes

return values for true elements of test.
no

return values for false elements of test.
Details

If yes or no are too short, their elements are recycled. yes will be
evaluated if and only if any element of test is true, and analogously for no
.

Missing values in test give missing values in the result.
Value

A vector of the same length and attributes (including dimensions and "class")
as test and data values from the values of yes or no. The mode of the
answer will be coerced from logical to accommodate first any values taken
from yes and then any values taken from no.
Warning

The mode of the result may depend on the value of test (see the examples),
and the class attribute (see
oldClass<http://127.0.0.1:27631/library/base/help/oldClass>)
of the result is taken from testand may be inappropriate for the values
selected from yes and no.

Sometimes it is better to use a construction such as

  (tmp <- yes; tmp[!test] <- no[!test]; tmp)

, possibly extended to handle missing values in test. References

Becker, R. A., Chambers, J. M. and Wilks, A. R. (1988) *The New S Language*.
Wadsworth & Brooks/Cole.
See Also

if <http://127.0.0.1:27631/library/base/help/if>.
Examples

x <- c(6:-4)
sqrt(x)  #- gives warning
sqrt(ifelse(x >= 0, x, NA))  # no warning

## Note: the following also gives the warning !
ifelse(x >= 0, sqrt(x), NA)

## example of different return modes:
yes <- 1:3
no <- pi^(0:3)
typeof(ifelse(NA, yes, no))    # logical
typeof(ifelse(TRUE, yes, no))  # integer
typeof(ifelse(FALSE, yes, no)) # double



On Mon, Dec 2, 2013 at 5:09 PM, Duncan Murdoch <murdoch.duncan@gmail.com>wrote:

> On 13-12-02 7:49 PM, Bill wrote:
>
>> It seems so inefficient. I mean the whole first vector will be
>> evaluated. Then if the second if is run the whole vector will be
>> evaluated again. Then if the next if is run the whole vector will be
>> evaluted again. And so on. And this could be only to test the first
>> element (if it is false for each if statement). Then this would be
>> repeated again and again. Is that really the way it works? Or am I not
>> thinking clearly?
>>
>
> Read the manual.
>
> Duncan Murdoch
>
>
>>
>> On Mon, Dec 2, 2013 at 4:48 PM, Duncan Murdoch <murdoch.duncan@gmail.com
>> <mailto:murdoch.duncan@gmail.com>> wrote:
>>
>>     On 13-12-02 7:33 PM, Bill wrote:
>>
>>         ifelse ((day_of_week == "Monday"),1,
>>             ifelse ((day_of_week == "Tuesday"),2,
>>             ifelse ((day_of_week == "Wednesday"),3,
>>             ifelse ((day_of_week == "Thursday"),4,
>>             ifelse ((day_of_week == "Friday"),5,
>>             ifelse ((day_of_week == "Saturday"),6,7)))))))
>>
>>
>>             In code like the above, day_of_week is a vector and so
>>         day_of_week ==
>>         "Monday" will result in a boolean vector. Suppose day_of_week is
>>         Monday,
>>         Thursday, Friday, Tuesday. So day_of_week == "Monday" will be
>>         True,False,False,False. I think that ifelse will test the first
>>         element and
>>         it will generate a 1. At this point it will not have run
>>         day_of_week ==
>>         "Tuesday" yet. Then it will test the second element of
>>         day_of_week and it
>>         will be false and this will cause it to evaluate day_of_week ==
>>         "Tuesday".
>>         My question would be, does the evaluation of day_of_week ==
>>         "Tuesday"
>>         result in the generation of an entire boolean vector (which
>>         would be in
>>         this case False,False,False,True) or does the ifelse "manage the
>>         indexing"
>>         so that it only tests the second element of the original vector
>>         (which is
>>         Thursday) and for that matter does it therefore not even bother
>>         to generate
>>         the first boolean vector I mentioned above
>>         (True,False,False,False) but
>>         rather just checks the first element?
>>             Not sure if I have explained this well but if you understand
>>         I would
>>         appreciate a reply.
>>
>>
>>     See the help for the function.  If any element of the test is true,
>>     the full first vector will be evaluated.  If any element is false,
>>     the second one will be evaluated.  There are no shortcuts of the
>>     kind you describe.
>>
>>     Duncan Murdoch
>>
>>
>>
>

	[[alternative HTML version deleted]]

