R/count_na.R

Defines functions count_na

Documented in count_na

#' @title Count NA values in a vector
#'
#' @param x a vector with data
#' @param mean a boolean, if \code{TRUE} instead of the sum the mean of \code{NA}s
#'  is calculated.
#'
#' @return returns either the sum or the mean of NA values.
#' @export
#' @author Jakob Gepp
#'
#' @examples
#' x <- c(NA, NA, 1, NaN, 0)
#' count_na(x)
#' # [1] 3
#'
#' x <- c(NA, NA, 1, NaN, "0")
#' count_na(x)
#' # [1] 2

count_na <- function(x, mean = FALSE) {
  out <- sum(is.na(x))

  if (mean) {
    out <- mean(is.na(x))
  }

  return(out)
}
STATWORX/helfRlein documentation built on Feb. 12, 2024, 2:21 a.m.