tidyverse / dtplyr

Data table backend for dplyr
https://dtplyr.tidyverse.org
Other
670 stars 57 forks source link

Translate `case_when(.default = )` #429

Closed markfairbanks closed 1 year ago

markfairbanks commented 1 year ago
library(dplyr, w = FALSE)
library(dtplyr)

df <- lazy_dt(tibble(x = 1:5))

df %>%
  mutate(case_x = case_when(x < 2 ~ 1,
                            x < 4 ~ 2,
                            TRUE ~ 3))
#> Source: local data table [5 x 2]
#> Call:   copy(`_DT1`)[, `:=`(case_x = fcase(x < 2, 1, x < 4, 2, rep(TRUE, 
#>     .N), 3))]
#> 
#>       x case_x
#>   <int>  <dbl>
#> 1     1      1
#> 2     2      2
#> 3     3      2
#> 4     4      3
#> 5     5      3
#> 
#> # Use as.data.table()/as.data.frame()/as_tibble() to access results

df %>%
  mutate(case_x = case_when(x < 2 ~ 1,
                            x < 4 ~ 2,
                            .default = 3))
#> Error in x[[2]]: subscript out of bounds

Created on 2023-03-29 with reprex v2.0.2