R/sum_NA.R

Defines functions sum_blank sum_NaN sum_NA

Documented in sum_blank sum_NA sum_NaN

#' Investigate missingness
#'
#' @param x a vector with multiple elements
#'
#' @details These functions return the number of NA, NaN, or blank values in a
#'   vector.  Such information can serve as a diagnostic or used outright, e.g.
#'   in longitudinal settings. If you are dealing with singletons, you should
#'   just use the underlying function, e.g. is.na. For total blank values, the
#'   stringi package is required.
#'
#'   For more in this realm, I suggest you visit
#'   \href{http://naniar.njtierney.com/}{naniar}.
#'
#' @return The number of non-elements so defined
#'
#' @examples
#' library(tidyext)
#' sum_blank(c('    ', '', 'c', 'd'))
#' sum_NA(c(NA, '', 'c', NA))
#' sum_NaN(c(1:3, NA, NaN))
#' sum_NA(1:4)
#'
#'
#'
#' @export
sum_NA <- function(x) {
  sum(is.na(x))
}

#' @rdname sum_NA
#' @export
sum_NaN <- function(x) {
  if (!is.numeric(x))
    stop('x must potentially be a number, in order to not be a number.
    ~ The Buddha')
  sum(is.nan(x))
}

#' @rdname sum_NA
#' @export
sum_blank <- function(x) {
  sum(stringi::stri_trim_both(x) == '')
}
m-clark/tidyext documentation built on July 21, 2020, 2:36 a.m.