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

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Sat 07 Oct 2006 - 13:01:47 GMT

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.  

> this must be rewritten as:
>
> n <- 1
> f <- cos
> if (n == 1) f <- sin
> f(pi)

No, it must not.

n <- 1
f <- if (n==1) sin else cos
f(pi)

or even

(if (n==1) sin else cos)(pi)

-- 
   O__  ---- Peter Dalgaard             Øster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
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 Sat Oct 07 23:05:19 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 Sat 07 Oct 2006 - 15:30:09 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.