From wdunlap at tibco.com  Fri Apr  8 18:22:08 2011
From: wdunlap at tibco.com (William Dunlap)
Date: Fri, 8 Apr 2011 09:22:08 -0700
Subject: [Rd] duplicates() function
In-Reply-To: <4D9F26A3.6060609@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>
	<4D9F26A3.6060609@gmail.com>
Message-ID: <77EB52C6DD32BA4D87471DCD70C8D700041B2D45@NA-PA-VBE03.na.tibco.com>

> -----Original Message-----
> From: r-devel-bounces at r-project.org 
> [mailto:r-devel-bounces at r-project.org] On Behalf Of Duncan Murdoch
> Sent: Friday, April 08, 2011 8:16 AM
> To: Joshua Ulrich
> Cc: R-devel at r-project.org
> Subject: Re: [Rd] duplicates() function
> 
> On 08/04/2011 11:08 AM, Joshua Ulrich wrote:
> > How about:
> >
> > y<- rep(NA,length(x))
> > y[duplicated(x)]<- match(x[duplicated(x)] ,x)
> 
> That's a nice solution for vectors.  Unfortunately for me, I have a 
> matrix (which duplicated() handles by checking whole rows).  

Does R have a function like match() that treats matrices
and data.frames row-wise, as duplicated() and unique() do?
duplicated() and match() do related things and I've been
annoyed that their methods for non-vectors do not match up
with each other.  (For historical reasons match cannot be
changed, but perhaps a new generic is in order.)

JU's code still would not work on matrices, but a variant could.

Bill Dunlap
Spotfire, TIBCO Software
wdunlap tibco.com 


> So a better 
> example that I should have posted would be
> 
> x <-  cbind(1, c(9,7,9,3,7) )
> 
> and I'd still like the same output
> 
> >  duplicated(x)
> [1] FALSE FALSE  TRUE FALSE TRUE
> 
> >  duplicates(x)
> [1] NA NA  1 NA  2
> 
> 
> Duncan Murdoch
> 
> > --
> > 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
William Dunlap