R/n_distinct.R

Defines functions n_distinct

Documented in n_distinct

#' Count the number of unique values in a set of vectors
#'
#' This is the equivalent of `length(unique(x))` for multiple vectors.
#'
#' @param ... Vectors of values.
#' @param na.rm `logical(1)`. If `TRUE` missing values don't count.
#'
#' @examples
#' x <- sample(1:10, 1e5, rep = TRUE)
#' length(unique(x))
#' n_distinct(x)
#'
#' @export
n_distinct <- function(..., na.rm = FALSE) {
  res <- do.call(cbind, list(...))
  if (isTRUE(na.rm)) res <- res[!is.na(res), , drop = FALSE]
  nrow(unique(res))
}

Try the poorman package in your browser

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

poorman documentation built on Nov. 2, 2023, 5:27 p.m.