From murdoch.duncan at gmail.com  Fri Apr  8 16:59:10 2011
From: murdoch.duncan at gmail.com (Duncan Murdoch)
Date: Fri, 08 Apr 2011 10:59:10 -0400
Subject: [Rd] duplicates() function
In-Reply-To: <B722DD6C-3B97-46EC-B369-8316F0F65D1F@cbs.dk>
References: <bug-14552-16@http.bugs.r-project.org/bugzilla3/>	<20110408142610.88AEC970477@ix.urbanek.info>
	<B722DD6C-3B97-46EC-B369-8316F0F65D1F@cbs.dk>
Message-ID: <4D9F22BE.5000502@gmail.com>

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



[Rd] duplicates() function
Duncan Murdoch