Sparklyr using case_when with variables
Asked Answered
C

1

5

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'))
Cudlip answered 10/10, 2017 at 23:19 Comment(0)
S
7

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

Seena answered 28/10, 2017 at 4:24 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.