jamborta
jamborta

Reputation: 5210

Return FALSE for duplicated NA values when using the function duplicated()

just wondering why duplicated behaves the way it does with NAs:

> duplicated(c(NA,NA,NA,1,2,2))
[1] FALSE  TRUE  TRUE FALSE FALSE  TRUE

where in fact

> NA == NA
[1] NA

is there a way to achieve that duplicated marks NAs as false, like this?

> duplicated(c(NA,NA,NA,1,2,2))
[1] FALSE  FALSE  FALSE FALSE FALSE  TRUE

Upvotes: 13

Views: 3910

Answers (1)

Joris Meys
Joris Meys

Reputation: 108533

You use the argument incomparables for the function duplicated like this :

> duplicated(c(NA,NA,NA,1,2,2))
[1] FALSE  TRUE  TRUE FALSE FALSE  TRUE
> duplicated(c(NA,NA,NA,1,2,2),incomparables=NA)
[1] FALSE FALSE FALSE FALSE FALSE  TRUE

It determines the values that cannot be compared (in this case NA) and returns FALSE for those values. See also ?duplicated

Upvotes: 25

Related Questions