case_when在变更管道 [英] case_when in mutate pipe
本文介绍了case_when在变更管道的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
在 dplyr :: mutate
调用中,似乎 dplyr :: case_when
不像其他命令一样。例如:
library(dplyr)
case_when(mtcars $ carb <= 2〜低,
mtcars $ carb> 2〜high)%>%
表
工作:
。
高低
15 17
但把 case_when
mutate chain:
mtcars %>%
/ pre>
mutate(cg = case_when(carb< = 2〜low,
carb> 2〜high))
,你会得到:
错误:对象' '没有找到
这个工作很好
mtcars%>%
mutate(cg = carb%>%
cut(c(0,2,8)))
解决方案我们可以使用
$ $ / code >
mtcars%>%
mutate(cg = case_when(。$ carb< = 2〜 $。$> 2〜high))%>%
。$ cg%>%
表()
#高低
#15 17
It seems
dplyr::case_when
doesn't behave as other commands in adplyr::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 amutate
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)))
解决方案We can use
.$
mtcars %>% mutate(cg = case_when(.$carb <= 2 ~ "low", .$carb > 2 ~ "high")) %>% .$cg %>% table() # high low # 15 17
这篇关于case_when在变更管道的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文