如何正确地将不等式或范围传递给dplyr :: case_when

时间:2019-03-03 10:27:33

标签: r dplyr tidyverse tidyr

当涉及间隔时,我无法尝试使用dplyr :: case_w创建给定变量的级别。

#准备样品数据

mtmodel <- lm(mpg ~ wt, data = mtcars)
mtcars$Low <- predict(mtmodel, newdata = mtcars, interval = "confidence")[,2]
mtcars$High <- predict(mtmodel, newdata = mtcars, interval = "confidence")[,3]
mtcars$Mean <- predict(mtmodel, newdata = mtcars, interval = "confidence")[,1]
new_mtcars<-gather(mtcars, "Variable", "value", Low:Mean)

#使用dplyr :: case_when创建组

#does not work
library(tidyverse)
new_new_mtcars<-new_mtcars %>%
       mutate(grouping = case_when (
       min(new_mtcars$wt) <= new_mtcars$wt<= mean(new_mtcars$wt)+0.99 ~ "group1",
       new_mtcars$wt >= max(new_mtcars$wt) - 0.5  ~ "group2"))

#R返回此错误消息,并且未按预期完成工作

Error: unexpected '<=' in:
"           mutate(grouping = case_when (
               min(new_mtcars$wt) <= new_mtcars$wt<="

Error: unexpected ')' in "           
new_mtcars$wt >= max(new_mtcars$wt) - 0.5  ~ "group2")"

1 个答案:

答案 0 :(得分:2)

尝试一下:

new_new_mtcars <- new_mtcars %>%
  mutate(grouping = case_when(
    min(wt) <= wt & wt <= mean(wt) + 0.99 ~ "group1",
    wt >= max(wt) - 0.5  ~ "group2"
  ))

第一次引用后,您无需在管道内引用数据框。此外,min(wt) <= wt <= mean(wt) + 0.99总是会引发错误,因为每个操作符始终需要两个变量,因此您需要将wt <= mean(wt) + 0.99指定为另一个条件。

如果您使用between之类的东西,则是一个例外,首先要声明中间的变量,然后是上下限,如下所示:

new_new_mtcars <- new_mtcars %>%
  mutate(grouping = case_when(
    between(wt, min(wt), mean(wt) + 0.99) ~ "group1",
    wt >= max(wt) - 0.5  ~ "group2"
  ))