It does work as documented. My question was why it was designed to work
this way. I can not think of a practical situation when someone might
want to ifelse() on a 'test' that is shorter than yes/no w/o expecting
'test' to recycle (therefore I was asking for a warning).
I find this behavior inconsistent with the (spirit of) R's recycling
rules. For example if 'test', 'yes', 'no' are all of the same length
then the following two expressions are equivalent:
1.
x - ifelse(test, yes, no)
2.
x - no; x[test] - yes[test]
This equivalence breaks when 'test' is shorter than yes/no: in the
second case 'test' will be recycled. And I don't see a good reason for
having them behave differently.
If I had to implement ifelse() I'd probably do:
ifelse2 - function(test, yes, no) {
x - rep(no, length.out=max(length(test), length(yes),
length(no)))
x[test] - yes[test]
x
}
(If there is interest I can extend it to take care of NA-s and submit as
a (trivial) patch)
Here is a simple test:
ifelse2(c(TRUE, FALSE), seq(10), -seq(5))
[1] 1 -2 3 -4 5 -1 7 -3 9 -5
Maybe it will help if I tell how I stumbled upon this problem. I had two
m*n matrices, 'yes' and 'no', and a 'test' vector of length m. I wanted
to create a m*n matrix which has 'yes' rows where test==TRUE and 'no'
rows otherwise. So I did
x - matrix(ifelse(test, yes, no), nrow(yes), ncol(yes))
priding myself for doing it the whole object way ... and 'test' did
not recycle (in full accordance with the help page) w/o a warning.
Thanks,
Vadim
-Original Message-
From: Liaw, Andy [mailto:[EMAIL PROTECTED]
Sent: Thursday, May 20, 2004 2:20 PM
To: Vadim Ogranovich; R-Help
Subject: RE: [R] ifelse when test is shorter than yes/no
From: Vadim Ogranovich
Hi,
It turns out that the 'test' vector in ifelse(test, yes, no) is not
recycled if it is shorter than the other arguments, e.g.
ifelse(TRUE, seq(10), -seq(10))
[1] 1
Is there any particular reason it is not recycled? If there is one
indeed a warning message might be in order when someone
calls ifelse
with a shorter 'test'.
?ifelse says:
Value:
A vector of the same length and attributes (including class) as
'test' and data values from the values of 'yes' or 'no'. ...
Seems to me it works as documented. Why do you expected otherwise?
Andy
This is R1.8.1 on RH-7.3
Thanks,
Vadim
[[alternative HTML version deleted]]
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide!
http://www.R-project.org/posting-guide.html
--
Notice: This e-mail message, together with any attachments,
contains information of Merck Co., Inc. (One Merck Drive,
Whitehouse Station, New Jersey, USA 08889), and/or its
affiliates (which may be known outside the United States as
Merck Frosst, Merck Sharp Dohme or MSD and in Japan, as
Banyu) that may be confidential, proprietary copyrighted
and/or legally privileged. It is intended solely for the use
of the individual or entity named on this message. If you
are not the intended recipient, and have received this
message in error, please notify us immediately by reply
e-mail and then delete it from your system.
--
__
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html