R/check_missing.R

Defines functions check_missing_ check_missing

Documented in check_missing check_missing_

#' Check missing rate in variables.
#'
#' Check missing (\code{NA}) proportion or counts of variables. This function
#' works like \code{\link[dplyr]{summarize_at}} where the missing rate or
#' count for the selected columns are returned.
#'
#'
#' @inheritParams common_params
#' @param ... Pass to tidyselect. See [dplyr::dplyr_tidy_select] for details.
#' @param ret_prop Whether to return the rate of missing (default) or counts.
#'
#' @author Min Ma
#' @export
check_missing <- function(data, ..., ret_prop = TRUE) {
  check_missing_(data, .dots = lazyeval::lazy_dots(...),
                 ret_prop = ret_prop)
}

#' @importFrom dplyr n
#' @describeIn check_missing SE version of check_missing.
#' @export
check_missing_ <- function(data, ..., .dots, ret_prop = TRUE) {
  dots <- lazyeval::all_dots(.dots, ...)
  vars <- dplyr::select_vars_(names(data), dots)

  if (ret_prop) {
    fun <- dplyr::funs_(quote(sum(is.na(.)) / n()))
  } else {
    fun <- dplyr::funs_(quote(sum(is.na(.))))
  }

  dplyr::summarise_at(data, .cols = vars, .funs = fun)
}

Try the extdplyr package in your browser

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

extdplyr documentation built on April 20, 2020, 9:04 a.m.