dplyr if_else() vs base R ifelse()

前端 未结 4 1149
野趣味
野趣味 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:32

    I'd also add that if_else() can attribute a value in case of NA, which is a handy way of adding an extra condition.

    df <- data_frame(val = c(80, 90, NA, 110))
    df %>% mutate(category = if_else(val < 100, 1, 2, missing = 9))
    
    #     val category
    #       
    # 1    80        1
    # 2    90        1
    # 3    NA        9
    # 4   110        2
    

提交回复
热议问题