(Background info: ifelse
evaluates both of the expressions, even though only one will be returned. EDIT: This is an incorrect statement. See Tommy\'s r
On your second point, how do you define "best"? I think ifelse()
is one of the more readable solutions, but may not always be the fastest. Specifically, I've found that writing out boolean conditions and adding them together can give you some performance benefits. Here's a quick example:
> x <- rnorm(1e6)
> system.time(y1 <- ifelse(x > 0,1,2))
user system elapsed
0.46 0.08 0.53
> system.time(y2 <- (x > 0) * 1 + (x <= 0) * 2)
user system elapsed
0.06 0.00 0.06
> identical(y1, y2)
[1] TRUE
So, if speed is your biggest concern, the boolean approach may be better. However, for most of my purposes - I've found ifelse()
quick enough and is easy to grok. Your miles may vary obviously.