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)))
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…