[R] duplicated function

Duncan Murdoch murdoch.duncan at gmail.com
Tue Nov 18 16:40:16 CET 2014


On 18/11/2014 10:23 AM, Dennis Fisher wrote:
> R 3.1.1
> OS X
>
> Colleagues
>
> When I use the duplicated function, I often need to find both the duplicates and the original element that was duplicated.  This can be accomplished with:
> 	duplicated(OBJECT) | duplicated(OBJECT, fromLast=TRUE)
>
>  From my perspective, an improvement in the duplicated function would be an option that accomplishes this with a single call to the function.  This could either be:
> 	1.  a new option: all=TRUE (pick whatever name makes sense)
> 	2.  allowing fromLast to take a new value (e.g., NA, in the spirit of the xpd option in par())
>
> If my suggestion would yield unintended consequences, it can certainly be ignored.

The duplicated() function is pretty fast, so what's wrong with your 
original version?  If you find it to be too much typing, wouldn't it be 
simplest to write your own function, e.g.

nonunique <- function(x) duplicated(x) | duplicated(x, fromLast=TRUE)

?

Something I've wanted more than once is a variation on duplicated that 
returns the index of the duplicated element, so for example

dupindex(c(7,7,7,2,3,2))

would return

0 1 1 0 0 4

or possibly

1 1 1 4 5 4

Duncan Murdoch



More information about the R-help mailing list