Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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'))
like image 538
rookie error Avatar asked Oct 10 '17 23:10

rookie error


1 Answers

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

like image 148
kevinykuo Avatar answered Nov 20 '22 10:11

kevinykuo