Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

case_when in mutate pipe

Tags:

r

dplyr

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))) 
like image 254
tomw Avatar asked Jul 29 '16 02:07

tomw


People also ask

What does Case_when do in R?

This function allows you to vectorise multiple if and else if statements. It is an R equivalent of the SQL CASE WHEN statement.

How to use case_ when()?

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 ( ~ ).

What is mutate ()?

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.


2 Answers

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

like image 79
George Wood Avatar answered Sep 30 '22 17:09

George Wood


We can use .$

mtcars %>%        mutate(cg = case_when(.$carb <= 2 ~ "low",  .$carb > 2 ~ "high")) %>%     .$cg %>%     table() # high  low  #  15   17  
like image 21
akrun Avatar answered Sep 30 '22 16:09

akrun