[R] Subsetting to unique values

Chuck Cleland ccleland at optonline.net
Fri Jun 6 16:08:34 CEST 2008


On 6/6/2008 9:35 AM, Emslie, Paul [Ctr] wrote:
> 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 <- data.frame(Id=c(1,1,2,2),name=c("Paul","Joe","Bob","Larry"))

!duplicated(ddTable$Id)
[1]  TRUE FALSE  TRUE FALSE

ddTable[!duplicated(ddTable$Id),]
   Id name
1  1 Paul
3  2  Bob

?duplicated

> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code. 

-- 
Chuck Cleland, Ph.D.
NDRI, Inc. (www.ndri.org)
71 West 23rd Street, 8th floor
New York, NY 10010
tel: (212) 845-4495 (Tu, Th)
tel: (732) 512-0171 (M, W, F)
fax: (917) 438-0894



More information about the R-help mailing list