Sparklyr 使用 case_when 和变量

Sparklyr using case_when with variables

case_when 与外部变量一起使用时,Sparklyr 失败。

工作示例:

test <- copy_to(sc, tibble(column = c(1,2,3,4)))
test %>%
  mutate(group = case_when(
                   column %in% c(1,2) ~ 'group 1',
                   column %in% c(3,4) ~ 'group 2'))

失败 Error: Can't extract an environment from NULL

test <- copy_to(sc, tibble(column = c(1,2,3,4)))
group1_cols <- c(1,2)
group2_cols <- c(3,4)
test %>%
  mutate(group = case_when(
                   column %in% group1_cols ~ 'group 1',
                   column %in% group2_cols ~ 'group 2'))

尝试取消引用变量:

test %>%
  mutate(group = case_when(
    column %in% !!group1_cols ~ 'group 1',
    column %in% !!group2_cols ~ 'group 2'))

有关详细信息,请查看 dplyr 编程插图 http://dplyr.tidyverse.org/articles/programming.html