[R] all duplicated wanted

peter dalgaard pdalgd at gmail.com
Fri Aug 3 09:31:53 CEST 2012


On Aug 3, 2012, at 09:06 , Weijia Wang wrote:

> Hi,
> 
> Has anyone been able to figure out how to print all duplicated observations?
> 
> I have a dataset, with patients ID, and other lab records.
> 
> Some patients have multiple lab records, but 'duplicated' ID will only show me the duplicates, not the original observation.
> 
> How can I print both the original one and the duplicates?

Something like this?

dd[ID %in% unique(ID[duplicated(ID)]),]

Let's try:

> ID <- sample(1:10, 10, replace=TRUE)
> table(ID)
ID
 1  2  3  4  7 10 
 1  1  3  1  2  2 
> ID[ID %in% unique(ID[duplicated(ID)])]
[1]  7  7 10  3  3  3 10

The unique() bit is really just for efficiency:

> ID[ID %in% ID[duplicated(ID)]]
[1]  7  7 10  3  3  3 10


-- 
Peter Dalgaard, Professor,
Center for Statistics, Copenhagen Business School
Solbjerg Plads 3, 2000 Frederiksberg, Denmark
Phone: (+45)38153501
Email: pd.mes at cbs.dk  Priv: PDalgd at gmail.com



More information about the R-help mailing list