n_distinct() counts the number of unique/distinct combinations in a set of one or more vectors. It's a faster and more concise equivalent to nrow(unique(data.frame(...))).
n_distinct(..., na.rm =FALSE)
Arguments
...: Unnamed vectors. If multiple vectors are supplied, then they should have the same length.
na.rm: If TRUE, exclude missing observations from the count. If there are multiple vectors in ..., an observation will be excluded if any of the values are missing.
Returns
A single number.
Examples
x <- c(1,1,2,2,2)n_distinct(x)y <- c(3,3,NA,3,3)n_distinct(y)n_distinct(y, na.rm =TRUE)# Pairs (1, 3), (2, 3), and (2, NA) are distinctn_distinct(x, y)# (2, NA) is dropped, leaving 2 distinct combinationsn_distinct(x, y, na.rm =TRUE)# Also works with data framesn_distinct(data.frame(x, y))