rookie error
rookie error

Reputation: 165

Sparklyr using case_when with variables

Sparklyr fails when using a case_when with external variables.

Working Example:

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'))

Fails with 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'))

Upvotes: 5

Views: 2135

Answers (1)

kevinykuo
kevinykuo

Reputation: 4772

Try unquoting the variables:

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

For more info check out the dplyr programming vignette http://dplyr.tidyverse.org/articles/programming.html

Upvotes: 7

Related Questions