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

Gabor Grothendieck ggrothendieck at gmail.com
Sat Oct 7 14:55:14 CEST 2006


Try

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


On 10/7/06, Alberto Vieira Ferreira Monteiro <albmont at centroin.com.br> wrote:
> Why this kind of assignment does not work?
>
>  n <- 1
>  f <- ifelse(n == 1, sin, cos)
>  f(pi)
>
> this must be rewritten as:
>
>  n <- 1
>  f <- cos
>  if (n == 1) f <- sin
>  f(pi)
>
> [oops. 1.224606e-16 instead of zero. Damn floating point errors :-/]
>
> Alberto Monteiro
>
> ______________________________________________
> R-help at 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.
>



More information about the R-help mailing list