case_when in mutate pipe

前端 未结 6 1096
无人共我
无人共我 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 17:59

    In addition to @akrun's answer above, be aware that the closing parenthesis for the case_when() cannot be put it onto its own line.

    For example, this works OK:

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

    but this does not:

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

提交回复
热议问题