[R] Subsetting to unique values

Adrian Dusa dusa.adrian at gmail.com
Fri Jun 6 16:38:50 CEST 2008


Emslie, Paul [Ctr] <emsliep <at> atac.mil> writes:

> 
> I want to take the first row of each unique ID value from a data frame.
> For instance
> > ddTable <-
> data.frame(Id=c(1,1,2,2),name=c("Paul","Joe","Bob","Larry"))
> 
> I want a dataset that is 
> Id	Name
> 1	Paul
> 2	Bob
> 
> > unique(ddTable)
> Will give me all 4 rows, and
> > unique(ddTable$Id) 
> Will give me c(1,2), but not accompanied by the name column.


ddTable[-which(duplicated(ddTable$Id)), ]

HTH,
Adrian



More information about the R-help mailing list