TysonStanley / tidyfast

Fast and efficient alternatives to tidyr functions built on data.table #rdatatable #rstats
https://tysonbarrett.com/tidyfast/
187 stars 4 forks source link

dt_case_when() doesn't handle is.na() conditions in some cases #29

Closed markfairbanks closed 4 years ago

markfairbanks commented 4 years ago
library(tidyfast)

x <- c(1, NA, 1, 2)

# Works
check1 <- dt_case_when(is.na(x) ~ 1,
                       x < 2 ~ 2,
                       TRUE ~ 0)

check1 == c(2,1,2,0)
#> [1] TRUE TRUE TRUE TRUE

# Fails
check2 <- dt_case_when(x < 2 ~ 2,
                       is.na(x) ~ 1,
                       TRUE ~ 0)

check2 == c(2,1,2,0)
#> [1] TRUE   NA TRUE TRUE
TysonStanley commented 4 years ago

Thanks for pointing this out. Will take a look and get it fixed 👍

TysonStanley commented 4 years ago

What is interesting is this is the behavior of data.table::fifelse() as well as base::ifelse() and dplyr::if_else(). I'm finding out why right now but thought this was interesting.

TysonStanley commented 4 years ago

I'm not convinced using data.table::fifelse() is worthwhile considering the new data.table::fcase(), which is already quite user friendly... I may end up dropping this one from tidyfast.