Re: [R] ifelse(logical, function1, function2) does not work

From: Thomas Lumley <tlumley_at_u.washington.edu>
Date: Mon 09 Oct 2006 - 14:55:03 GMT

On Sat, 7 Oct 2006, Rolf Turner wrote:

> Peter Dalgaard writes:
>
>> Alberto Vieira Ferreira Monteiro <albmont@centroin.com.br> writes:
>>
>>> Why this kind of assignment does not work?
>>>
>>> n <- 1
>>> f <- ifelse(n == 1, sin, cos)
>>> f(pi)
>>
>> It's not supposed to.
>>
>> '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'.
>>
>> which makes very little sense if yes and no are functions.
>
> I think that's a debatable assertion. Why might I
> not want a vector, or rather a list, of functions?
>

You might want a list of functions, but in that case ifelse() would have to return a list of functions, and in the example in question it would return list(sin) rather than sin.

There is a real difference between lists of functions and the things that rep() works on, in that a vector of one number is the same as that number, but a list of one function is not the same as the function.

         -thomas

Thomas Lumley			Assoc. Professor, Biostatistics
tlumley@u.washington.edu	University of Washington, Seattle

______________________________________________
R-help@stat.math.ethz.ch 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. Received on Tue Oct 10 01:09:00 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Mon 09 Oct 2006 - 15:30:10 GMT.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.