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