On 10/07/2019 11:54 a.m., Richard O'Keefe wrote:
Expectation: ifelse will use the same "repeat vectors to match the longest"
rule that other vectorised functions do. So
a <- 1:5
b <- c(2,3)
ifelse(a < 3, 1, b)
=> ifelse(T T F F F <<5>>, 1 <<1>>, 2 3 <<2>>)
=> ifelse(T T F F F <<5>>, 1 1 1 1 1 <<5>>, 2 3 2 3 2 <<5>>)
=> 1 1 2 3 2
and that is indeed the answer you get. Entirely predictable and consistent
with
other basic operations in R.
The only tricky thing I see is that R has
a strict vectorised ifelse(logical.vector, some.vector, another.vector)
AND
a non-strict non-vectorised if (logical.scalar) some.value else
another.value
AND
a statement form if (logical.scalar) stmt.1; else stmt.2;
That last one is not R syntax. The semicolon in R separates statements,
and "else stmt.2" is not a valid statement.
I'm sure that's just a typo, but I can't think of a variation on it that
is different from the one before.
Duncan Murdoch
______________________________________________
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.