It seems dplyr::case_when
doesn't behave as other commands in a dplyr::mutate
call. For instance:
library(dplyr) case_when(mtcars$carb <= 2 ~ "low", mtcars$carb > 2 ~ "high") %>% table
works:
. high low 15 17
But put case_when
in a mutate
chain:
mtcars %>% mutate(cg = case_when(carb <= 2 ~ "low", carb > 2 ~ "high"))
and you get:
Error: object 'carb' not found
while this works fine
mtcars %>% mutate(cg = carb %>% cut(c(0, 2, 8)))
This function allows you to vectorise multiple if and else if statements. It is an R equivalent of the SQL CASE WHEN statement.
case_when with a single case To do this syntactically, we simply type the name of the function: case_when() . Then, inside the parenthesis, there is an expression with a “left hand side” and a “right hand side,” which are separated by a tilde ( ~ ).
mutate() adds new variables and preserves existing ones; transmute() adds new variables and drops existing ones. New variables overwrite existing variables of the same name.
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
We can use .$
mtcars %>% mutate(cg = case_when(.$carb <= 2 ~ "low", .$carb > 2 ~ "high")) %>% .$cg %>% table() # high low # 15 17
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With