[R] need help in if else condition

Duncan Murdoch murdoch@dunc@n @end|ng |rom gm@||@com
Wed Jul 10 18:24:09 CEST 2019


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



More information about the R-help mailing list