is ifelse ever appropriate in a non-vectorized situation and vice-versa?

前端 未结 2 1253
萌比男神i
萌比男神i 2020-12-19 03:24

(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

2条回答
  •  挽巷
    挽巷 (楼主)
    2020-12-19 03:59

    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.

提交回复
热议问题