R/nmissing.R

Defines functions n_total n_not_missing n_missing

Documented in n_missing n_not_missing n_total

#' Counting missing/non-missing elements
#'
#' Counting missing/non-missing elements
#'
#' @param type one of `"any"` (default) or `"all"`.
#' @param ... vectors of equal length to be checked in parallel for missing values.
#' @return a vector of counts of missing or non-missing values.
#'
#' @export
#' @examples
#' if (require(NHANES) && require(mosaic) && require(dplyr)) {
#'   mosaic::tally( ~ is.na(Height) + is.na(Weight), data = NHANES, margins = TRUE)
#'   NHANES |>
#'     summarise(
#'       mean.wt = mean(Weight, na.rm = TRUE),
#'       missing.Wt = n_missing(Weight),
#'       missing.WtAndHt = n_missing(Weight, Height, type = "all"),
#'       missing.WtOrHt = n_missing(Weight, Height, type = "any")
#'       )
#'     }

n_missing <- function( ..., type = c("any", "all")) {
  type <- match.arg(type)
  M <- cbind(...)
  base::sum(
    apply(M, 1, function(x) do.call(type, list(is.na(x)))),
    na.rm = TRUE)
}

#' @rdname n_missing
#' @export
n_not_missing <- function( ..., type = c("any", "all")) {
  type <- match.arg(type)
  M <- cbind(...)
  base::sum(
    1 - apply(M, 1, function(x) do.call(type, list(is.na(x)))),
    na.rm = TRUE)
}

#' @rdname n_missing
#' @export
n_total <- function( ..., type = c("any", "all")) {
  M <- cbind(...)
  nrow(M)
}

Try the mosaicCore package in your browser

Any scripts or data that you put into this service are public.

mosaicCore documentation built on Nov. 5, 2023, 9:06 a.m.