number of unique elements excluding NA in r
You can use na.omit
first:
x <- c(1,2,3,NA,2)
length(unique(na.omit(x)))
Alternatively, n_distinct
from dplyr has an na_rm
argument:
library(dplyr)
n_distinct(x, na.rm = TRUE)
data.table::uniqueN
has na.rm
in version v1.9.7+.