Alberto Vieira Ferreira Monteiro <[EMAIL PROTECTED]> 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
~~~~~~~~~~ - ([EMAIL PROTECTED])                  FAX: (+45) 35327907

______________________________________________
[email protected] 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.

Reply via email to