Skip to contents

Returns the number of unique vector elements.

Usage

nUnique(x, na.rm = FALSE)

Arguments

x

a vector.

na.rm

logical. Should missing values (NA) be removed before counting unique values? Defaults to FALSE.

Value

an integer of length one.

See also

nlevels(), isLowCardinality() to check whether x has at most a given number of unique values, without counting all of them first.

Other data.predicate: flags(), isDichotomous(), isEuclid(), isLowCardinality(), isNumeric(), isWholeLike(), isZero()

Examples

nUnique(c(1, 1, 2, 3))
#> [1] 3

nUnique(c(1, 1, 2, NA))
#> [1] 3

nUnique(c(1, 1, 2, NA), na.rm = TRUE)
#> [1] 2