There were several interesting points about `ifelse`. The usual behaviour seems 
to be that all three inputs are evaluated, and the entries of `yes` 
corresponding to `TRUE` in `test` are combined with the entries of `no` 
corresponding to `FALSE` in `test`. Moreover, `yes` & `no` seem to be recycled 
as necessary in case `test` is longer. On top of that, there seems to be some 
sugar that suppresses evaluations in case `all(test)` and/or `all(!test)`, and 
the return type can be `logical` even if `yes` & `no` are not. I agreed with 
the other responses already, but my experiments further confirmed that `ifelse` 
is not interchangeable with `if(....) .... else ....`.



The documentation confirms most of this, but 'same length and attributes 
(including dimensions and �"class"�) as �test�' looks wrong. The output seems 
to be `logical` or something related to the classes of `yes` & `no`.



Regards,

Jorgen Harmse.



> ifelse(FALSE, {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'else'.

[1] 0

> ifelse(rep(FALSE,5L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'else'.

[1] 0 1 2 3 4

> ifelse(rep(TRUE,3L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'if'.

[1] 1 2 3

> ifelse(c(TRUE,TRUE,FALSE), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'if'.

Evaluating the vector for 'else'.

[1] 1 2 2

> ifelse(c(TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector for 'if'.\n"); 
> 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'if'.

Evaluating the vector for 'else'.

[1] 1 2 2 1

> args(ifelse)

function (test, yes, no)

NULL

> ifelse(c(TRUE,TRUE,FALSE,TRUE,TRUE,FALSE,TRUE), {cat("Evaluating the vector 
> for 'if'.\n"); 1:3}, {cat("Evaluating the vector for 'else'.\n"); 0:4})

Evaluating the vector for 'if'.

Evaluating the vector for 'else'.

[1] 1 2 2 1 2 0 1

> ifelse(logical(0L), {cat("Evaluating the vector for 'if'.\n"); 1:3}, 
> {cat("Evaluating the vector for 'else'.\n"); 0:4})

logical(0)

> ifelse(TRUE, integer(0L), numeric(0L))

[1] NA

> class(ifelse(TRUE, integer(0L), numeric(0L)))

[1] "integer"

> ifelse(integer(0L)) # test is an empty vector of integers and yes & no are 
> missing.

logical(0)








        [[alternative HTML version deleted]]

______________________________________________
R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see
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