R/n-prop-miss-complete.R

Defines functions pct_complete prop_complete pct_miss prop_miss n_complete n_miss

Documented in n_complete n_miss pct_complete pct_miss prop_complete prop_miss

#' Return the number of missing values
#'
#' Substitute for `sum(is.na(data))`
#'
#' @param x a vector
#'
#' @return numeric the number of missing values
#'
#' @export
#'
#' @examples
#'
#' n_miss(airquality)
#' n_miss(airquality$Ozone)
#'
n_miss <- function(x){
  test_if_null(x)
  sum(is.na(x))
}

#' Return the number of complete values
#'
#' A complement to `n_miss`
#'
#' @param x a vector
#'
#' @return numeric number of complete values
#'
#' @export
#'
#' @examples
#'
#' n_complete(airquality)
#' n_complete(airquality$Ozone)
#'
n_complete <- function(x){
  test_if_null(x)
  # number of total elements - number of missings
  sum(!is.na(x))

}
#' Return the proportion of missing values
#'
#' This is shorthand for `mean(is.na(x))`
#'
#' @param x vector or data.frame
#'
#' @return numeric the proportion of missing values in x
#'
#' @export
#'
#' @examples
#'
#' prop_miss(airquality)
#' prop_miss(airquality$Ozone)
#'
prop_miss <- function(x){
  test_if_null(x)
  mean(is.na(x))
}

#' Return the percent of missing values
#'
#' This is shorthand for `mean(is.na(x)) * 100`
#'
#' @param x vector or data.frame
#'
#' @return numeric the percent of missing values in x
#'
#' @export
#'
#' @examples
#'
#' pct_miss(airquality)
#' pct_miss(airquality$Ozone)
#'
pct_miss <- function(x){
  prop_miss(x) * 100
}

#' Return the proportion of complete values
#'
#' The complement to `prop_miss`
#'
#' @param x vector or data.frame
#'
#' @return numeric proportion of complete values
#'
#' @export
#'
#' @examples
#'
#' prop_complete(airquality)
#' prop_complete(airquality$Ozone)
#'
prop_complete <- function(x){
  test_if_null(x)
  # 1 - proportion of missings
  1 - mean(is.na(x))

}

#' Return the percent of complete values
#'
#' The complement to `pct_miss`
#'
#' @param x vector or data.frame
#'
#' @return numeric percent of complete values
#'
#' @export
#'
#' @examples
#'
#' pct_complete(airquality)
#' pct_complete(airquality$Ozone)
#'
pct_complete <- function(x){
  test_if_null(x)
  prop_complete(x) * 100

}

Try the naniar package in your browser

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

naniar documentation built on May 29, 2024, 1:43 a.m.