David Winsemius wrote:
On Oct 5, 2009, at 5:38 AM, Martin Maechler wrote:
"DW" == David Winsemius <dwinsem...@comcast.net>
on Sat, 3 Oct 2009 12:56:51 -0400 writes:
DW> On Oct 3, 2009, at 11:54 AM, Chen Gu wrote:
Hello,
I am doing a simple if else statement in R. But it always comes out
error
such as 'unexpected error'
There are two variables. ini and b. when ini=1, a=3; when ini>1 and
b> 2,
a=5; all other situations, a=6. I don't know where it is wrong.
Here is my code
ini=3
b=4
DW> Your basic problem is that you are confusing if and else which are
DW> program control functions with ifelse which is designed for
assignment
DW> purposes;
David, not quite: in R, "everything"(*) is a function,
and in the example we have here
(and innumerous similar examples) ifelse(.) is not efficient
to use:
ini=3
b=4
a <- ifelse( ini==1, 3, ifelse( ini>1 & b>2 , 5, 6))
a
[1] 5
More efficient -- and also nicer in my eyes ---
is
a <- if(ini == 1) 3 else if(ini > 1 && b > 2) 5 else 6
No argument that is a bit more readable than my version above. I guessed
that:
a <- 6 - 3*(ini == 1) - (ini > 1 && b > 2)
... might be even more efficient, ... but 10^6 instances took almost
twice as long as the if(){}else{} construction. The if else construction
took 2e-06 seconds, the ifelse(,,) took 4.2e-05 seconds and the Boolean
math version 3.45e-06 seconds.
As I say on many occasions:
ifelse() is useful and nice, but it is used in many more
places than it should.... {BTW, not only because of examples like
the above}.
On the r-help list I see many more examples where (I thought) ifelse
should have been used. I would be interested in seeing the other
examples that you have in mind where it should not be used. On the
principle that there are no new questions on r-help anymore, perhaps a
link to a discussion from the archives could be .... more efficient? (I
did try searching and have found some interesting material, but not on
this particular point. A search for:
"if else" ifelse efficien*
came up empty when restricted to R-help.)
The first rule is easy: As long as you are using scalar valued (i.e.
length 1 vectors in R) "cond", you should prefer
if(cond) cons.expr else alt.expr
rather than
ifelse(cond, yes, no)
because the latter one evaluates both "yes" and "no" while the former
one evaluates exactly one of both expressions.
Moreover, if "yes" and "no" are not really trivial, the penalty of
evaluating both of them should be considered.
Best,
Uwe Ligges
Martin Maechler, ETH Zurich
----
(*) "almost"
--
David Winsemius, MD
Heritage Laboratories
West Hartford, CT
______________________________________________
R-help@r-project.org 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.
______________________________________________
R-help@r-project.org 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.