case_when in mutate pipe

前端 未结 6 1105
无人共我
无人共我 2020-11-27 17:40

It seems dplyr::case_when doesn\'t behave as other commands in a dplyr::mutate call. For instance:

library(dplyr)

case_when(mtcars         


        
6条回答
  •  刺人心
    刺人心 (楼主)
    2020-11-27 18:03

    We can use .$

    mtcars %>%  
         mutate(cg = case_when(.$carb <= 2 ~ "low",  .$carb > 2 ~ "high")) %>%
        .$cg %>%
        table()
    # high  low 
    #  15   17 
    

提交回复
热议问题