Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

number of unique elements excluding NA in r

Tags:

r

I am looking to count the number of unique elements in a vector, but excluding NA elements.

Basically, I want to use something like length(unique(x)) with an na.rm=TRUE argument, so that if I have length(unique(c(1,2,3,NA,2))) will return 3

I tried data.table uniqueN but this also doesn't have this option. Is there a quick and easy way to do this, instead of having to do two separate operations on the column?

like image 780
Allen Wang Avatar asked Nov 06 '15 21:11

Allen Wang


2 Answers

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)
like image 194
David Robinson Avatar answered Nov 03 '22 00:11

David Robinson


data.table::uniqueN has na.rm in version v1.9.7+.

like image 28
Matt Dowle Avatar answered Nov 03 '22 01:11

Matt Dowle