From kasperdanielhansen at gmail.com  Fri Apr  8 17:21:00 2011
From: kasperdanielhansen at gmail.com (Kasper Daniel Hansen)
Date: Fri, 8 Apr 2011 11:21:00 -0400
Subject: [Rd] duplicates() function
In-Reply-To: <BANLkTimQEeXMPCjWaHuQZ74t8d0A_9XeyQ@mail.gmail.com>
References: <bug-14552-16@http.bugs.r-project.org/bugzilla3/>
	<20110408142610.88AEC970477@ix.urbanek.info>
	<B722DD6C-3B97-46EC-B369-8316F0F65D1F@cbs.dk>
	<4D9F22BE.5000502@gmail.com>
	<BANLkTimQEeXMPCjWaHuQZ74t8d0A_9XeyQ@mail.gmail.com>
Message-ID: <BANLkTin1iSGkxtb0w+gaDc8e2Cw4oTHBTQ@mail.gmail.com>

On Fri, Apr 8, 2011 at 11:08 AM, Joshua Ulrich <josh.m.ulrich at gmail.com> wrote:
> How about:
>
> y <- rep(NA,length(x))
> y[duplicated(x)] <- match(x[duplicated(x)] ,x)
>

I use Joshua's trick all the time.  But it might still be nice with a
C implementation.

While we are discussing duplication, I would also like to see
something like duplicated() but which returns TRUE whenever a value is
later duplicated, so I can easily select the values of a vector which
has are never duplicated.  Right now I need to do something like
  y [ ! y %in% y[duplicated(y)] ]
I am only bringing this up because of Duncan's request.

Kasper






> --
> Joshua Ulrich ?| ?FOSS Trading: www.fosstrading.com
>
>
>
> On Fri, Apr 8, 2011 at 9:59 AM, Duncan Murdoch <murdoch.duncan at gmail.com> wrote:
>> I need a function which is similar to duplicated(), but instead of returning
>> TRUE/FALSE, returns indices of which element was duplicated. ?That is,
>>
>>> x <- c(9,7,9,3,7)
>>> duplicated(x)
>> [1] FALSE FALSE ?TRUE FALSE TRUE
>>
>>> duplicates(x)
>> [1] NA NA ?1 NA ?2
>>
>> (so that I know that element 3 is a duplicate of element 1, and element 5 is
>> a duplicate of element 2, whereas the others were not duplicated according
>> to our definition.)
>>
>> Is there a simple way to write this function? ?I have ?an ugly
>> implementation in R that loops over all the values; it would make more sense
>> to redo it in C, if there isn't a simple implementation I missed.
>>
>> Duncan Murdoch
>>
>> ______________________________________________
>> R-devel at r-project.org mailing list
>> https://stat.ethz.ch/mailman/listinfo/r-devel
>>
>
> ______________________________________________
> R-devel at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-devel
>



[Rd] duplicates() function
Kasper Daniel Hansen