[R] duplicated function

John Laing john.laing at gmail.com
Tue Nov 18 17:22:58 CET 2014


That seems straightforward enough:
> x <- c(7, 7, 7, 2, 3, 2)
> match(x, x)
[1] 1 1 1 4 5 4
> ifelse(duplicated(x), match(x, x), 0)
[1] 0 1 1 0 0 4

On Tue, Nov 18, 2014 at 10:40 AM, Duncan Murdoch
<murdoch.duncan at gmail.com> wrote:
> 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
>
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list