R/n_distinct.R

Defines functions n_distinct. n_distinct

Documented in n_distinct n_distinct.

#' Count the number of unique values in a vector
#'
#' @description
#' This is a faster version of `length(unique(x))` that calls `data.table::uniqueN()`.
#'
#' @param ... vectors of values
#' @param na.rm  If `TRUE` missing values don't count
#'
#' @export
#'
#' @examples
#' x <- sample(1:10, 1e5, rep = TRUE)
#' n_distinct(x)
n_distinct <- function(..., na.rm = FALSE) {
  dots <- list2(...)

  if (length(dots) == 1) {
    x <- dots[[1]]
  } else {
    x <- new_data_frame(dots)
  }

  uniqueN(x, na.rm = na.rm)
}

#' @export
#' @keywords internal
#' @inherit n_distinct
n_distinct. <- function(..., na.rm = FALSE) {
  deprecate_dot_fun()
  n_distinct(..., na.rm = na.rm)
}

Try the tidytable package in your browser

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

tidytable documentation built on Oct. 5, 2023, 5:07 p.m.