dplyr if_else() vs base R ifelse()

前端 未结 4 1152
野趣味
野趣味 2020-11-30 10:22

I am fairly proficient within the Tidyverse, but have always used ifelse() instead of dplyr if_else(). I want to switch this behavior and default t

4条回答
  •  没有蜡笔的小新
    2020-11-30 10:45

    Another important reason for preferring if_else() to ifelse() is checking for consistency in lengths. See this dangerous gotcha:

    > tibble(x = 1:3, y = ifelse(TRUE, x, 4:6))
    # A tibble: 3 x 2
          x     y
       
    1     1     1
    2     2     1
    3     3     1
    

    Compare with

    > tibble(x = 1:3, y = if_else(TRUE, x, 4:6))
        Error: `true` must be length 1 (length of `condition`), not 3.
    

    The intention in both cases is clearly for column y to equal x or to equal 4:6 acording to the value of a single (scalar) logical variable; ifelse() silently truncates its output to length 1, which is then silently recycled. if_else() catches what is almost certainly an error at source.

提交回复
热议问题