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

纵饮孤独 提交于 2019-12-23 09:19:29

问题


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

回答1:


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



来源:https://stackoverflow.com/questions/13583523/return-false-for-duplicated-na-values-when-using-the-function-duplicated

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!