Sparklyr使用case_when和变量

时间:2017-10-10 23:19:24

标签: r dplyr sparklyr dbplyr

当使用带有外部变量的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'))

1 个答案:

答案 0 :(得分:3)

尝试取消引用变量:

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