R/missing.R

Defines functions missingness

Documented in missingness

#' Summarize missing values by column or row
#'
#' @param data A data.frame.
#' @param margin Summarize missingness `"column"`-wise (the default) or
#'   `"row"`-wise.
#'
#' @return A basetable with one row per column (or per row) describing the count
#'   and proportion of missing values.
#' @export
missingness <- function(data, margin = c("column", "row")) {
  margin <- match.arg(margin)
  df <- bt_as_data_frame(data)

  if (margin == "column") {
    return(bt_as_data_table(data.frame(
      column = names(df),
      missing = vapply(df, function(x) sum(bt_is_blank(x)), numeric(1)),
      missing_prop = vapply(df, function(x) mean(bt_is_blank(x)), numeric(1)),
      complete = vapply(df, function(x) sum(!bt_is_blank(x)), numeric(1)),
      stringsAsFactors = FALSE
    )))
  }

  miss_mat <- do.call(cbind, lapply(df, bt_is_blank))
  bt_as_data_table(data.frame(
    row = seq_len(nrow(df)),
    missing = rowSums(miss_mat),
    complete = rowSums(miss_mat) == 0L,
    stringsAsFactors = FALSE
  ))
}

Try the basetable package in your browser

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

basetable documentation built on Sept. 18, 2026, 9:13 a.m.