Skip to content

Commit

Permalink
added check for NA factor levels
Browse files Browse the repository at this point in the history
  • Loading branch information
ddsjoberg committed Jun 20, 2024
1 parent e48f687 commit 07cd51e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
16 changes: 16 additions & 0 deletions R/ard_categorical.survey.design.R
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ ard_categorical.survey.design <- function(data,
# if no variables selected, return empty data frame
if (is_empty(variables)) return(dplyr::tibble()) # styler: off

check_na_factor_levels(data$variables, c(by, variables))

cards::process_formula_selectors(
data = data$variables[variables],
statistic = statistic,
Expand Down Expand Up @@ -188,6 +190,20 @@ ard_categorical.survey.design <- function(data,
cards::tidy_ard_row_order()
}

# check for functions with NA factor levels (these are not allowed)
check_na_factor_levels <- function(data, variables) {
walk(
variables,
\(variable) {
if (is.factor(data[[variable]]) && any(is.na(levels(data[[variable]])))) {
cli::cli_abort(
"Factors with {.val {NA}} levels are not allowed, which are present in column {.val {variable}}.",
call = get_cli_abort_call()
)
}
}
)
}

# this function returns a tibble with the SE(p) and DEFF
.svytable_rate_stats <- function(data, variables, by, denominator, deff) {
Expand Down
2 changes: 2 additions & 0 deletions R/ard_continuous.survey.design.R
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ ard_continuous.survey.design <- function(data, variables, by = NULL,
# process inputs -------------------------------------------------------------
cards::process_selectors(data$variables, variables = {{ variables }}, by = {{ by }})
variables <- setdiff(variables, by)
check_na_factor_levels(data$variables, by)

cards::process_formula_selectors(
data$variables[variables],
statistic = statistic,
Expand Down

0 comments on commit 07cd51e

Please sign in to comment.