case_when in mutate pipe

前端 未结 6 1094
无人共我
无人共我 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

    As of version 0.7.0 of dplyr, case_when works within mutate as follows:

    library(dplyr) # >= 0.7.0
    mtcars %>% 
      mutate(cg = case_when(carb <= 2 ~ "low",
                            carb > 2  ~ "high"))
    

    For more information: http://dplyr.tidyverse.org/reference/case_when.html

提交回复
热议问题