assertr icon indicating copy to clipboard operation
assertr copied to clipboard

predicate in_set inverse with missing values

Open AnthonyEbert opened this issue 1 month ago • 0 comments

With the predicate in_set with argument inverse=TRUE, the argument allow.na works in the opposite way to what is expected.

library(dplyr)
library(survival)

veteran_totals = veteran %>%
  group_by(celltype) %>%
  summarise(cell_total = n())
  
veteran_totals$cell_total[1] <- NA
 
veteran_totals %>% assertr::assert(cell_total, predicate = assertr::in_set(10:20, inverse = TRUE, allow.na = TRUE)) 
>Column 'cell_total' violates assertion 'assertr::in_set(10:20, inverse = TRUE, allow.na = TRUE)' 1 time
>  verb redux_fn                                               predicate     column index value
>1 assert       NA assertr::in_set(10:20, inverse = TRUE, allow.na = TRUE) cell_total     1    NA
>
>Error: assertr stopped execution

veteran_totals %>% assertr::assert(cell_total, predicate = assertr::in_set(10:20, inverse = TRUE, allow.na = FALSE))
> # A tibble: 4 × 2
> celltype  cell_total
>  <fct>          <int>
>1 squamous          NA
>2 smallcell         48
>3 adeno             27
>4 large             27

I guess you could fix this with a line allow.na = ifelse(inverse, !allow.na, allow.na) in the first line of the function. ?

AnthonyEbert avatar May 24 '24 11:05 AnthonyEbert