Skip to content

Commit

Permalink
adding na_check for internal use
Browse files Browse the repository at this point in the history
  • Loading branch information
lilyclements committed Feb 19, 2025
1 parent 75923a7 commit 286fc75
Show file tree
Hide file tree
Showing 2 changed files with 97 additions and 0 deletions.
50 changes: 50 additions & 0 deletions R/summary_functions.R
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,53 @@ summary_sum <- function (x, weights = NULL, na.rm = FALSE, na_type = "", ...) {
else return(sum(x * weights, na.rm = na.rm))
}
}

#' Check Missing Values Based on Conditions
#'
#' Evaluates a vector against specified conditions for missing values.
#'
#' @param x A vector to check for missing values.
#' @param na_type A character vector specifying the types of checks to perform. Options include:
#' \itemize{
#' \item `"n"`: Total number of missing values (`<= na_max_n`).
#' \item `"prop"`: Proportion of missing values (`<= na_max_prop` in percentage).
#' \item `"n_non_miss"`: Minimum number of non-missing values (`>= na_min_n`).
#' \item `"FUN"`: A custom function to evaluate missing values.
#' \item `"con"`: Maximum consecutive missing values (`<= na_consecutive_n`).
#' }
#' @param na_consecutive_n Optional. Maximum allowed consecutive missing values.
#' @param na_max_n Optional. Maximum allowed missing values.
#' @param na_max_prop Optional. Maximum allowed proportion of missing values (in percentage).
#' @param na_min_n Optional. Minimum required non-missing values.
#' @param na_FUN Optional. A custom function to evaluate missing values.
#' @param ... Additional arguments passed to the custom function `na_FUN`.
#' @return Logical. Returns `TRUE` if all specified checks pass, otherwise `FALSE`.
na_check <- function(x, na_type = c(), na_consecutive_n = NULL, na_max_n = NULL, na_max_prop = NULL, na_min_n = NULL, na_FUN = NULL, ...) {
res <- c()
for (i in seq_along(na_type)) {
type <- na_type[i]
if (type %in% c("n","'n'")) {
res[i] <- summary_count_miss(x) <= na_max_n
}
else if (type %in% c("prop","'prop'")) {
res[i] <- (summary_count_miss(x) / summary_count(x)) <= na_max_prop / 100
}
else if (type %in% c("n_non_miss","'n_non_miss'")) {
res[i] <- summary_count(x) >= na_min_n
}
else if (type %in% c("FUN","'FUN'")) {
res[i] <- na_FUN(x, ...)
}
else if (type %in% c("con","'con'")) {
is_na_rle <- rle(is.na(x))
res[i] <- max(is_na_rle$lengths[is_na_rle$values]) <= na_consecutive_n
}
else {
stop("Invalid na_type specified for missing values check.")
}
if (!res[i]) {
return(FALSE)
}
}
return(all(res))
}
47 changes: 47 additions & 0 deletions man/na_check.Rd

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit 286fc75

Please sign in to comment.