Message-ID: <Pine.A41.4.61b.0505310838360.190182@homer05.u.washington.edu>
Date: 2005-05-31T15:45:56Z
From: Thomas Lumley
Subject: A suggestion to improve ifelse behaviour with vector yes/no arguments
In-Reply-To: <429C7FE0.6010302@stats.uwo.ca>
On Tue, 31 May 2005, Duncan Murdoch wrote:
> M?kinen Jussi wrote:
>> Dear All,
>>
>> I luckily found the following feature (or problem) when tried to apply
>> ifelse-function to an ordered data.
>>
>>
>>> test <- c(TRUE, TRUE, TRUE, FALSE, FALSE, FALSE, FALSE)
>>> ifelse(test, 0, 1:4)
>>
>> [1] 0 0 0 4 1 2 3
>>
<snippage>
>
> As Dimitris said, this is just recycling. I think getting rid of recycling
> on vectors with length greater than 1 would have been a good decision in S
> about 15 years ago, but it's too late now.
It wouldn't help the original poster, though. I agree that 0,0,0,4,1,2,3
is a slightly weird result, but I can't think of any reasonable model for
the behaviour of ifelse() that would give any other result except an error
message. [or 0,NA,NA,4,NA,NA,NA, I suppose].
-thomas