[R] Unique Pairs based on a Minimum Value
Chuck Cleland
ccleland at optonline.net
Tue Nov 6 02:04:42 CET 2007
Bret Collier wrote:
> RUsers,
>
> I am trying (with little success) to determine how to combine unique and
> min to do some data frame manipulations and I could use a little advice.
> I have tried various combinations of 'unique', 'min' and 'apply' with
> no luck.
>
> Given this simple example data (x, y)
>
> x<-c(10, 10, 12, 13, 15, 16 ,17, 17, 17)
> y<-c(112, 117, 111, 117, 114, 113, 119, 121, 130)
>
> as.data.frame(cbind(x, y))
>
> x y
> 1 10 112
> 2 10 117
> 3 12 111
> 4 13 117
> 5 15 114
> 6 16 113
> 7 17 119
> 8 17 121
> 9 17 130
>
> I have been attempting to get all those unique 'x-y' combinations for
> which the y column is the minimum of the values y takes for each unique
> x (ID variable), such as below.
>
> x y
> 1 10 112
> 3 12 111
> 4 13 117
> 5 15 114
> 6 16 113
> 7 17 119
>
> Any advice/directions I could look would be appreciated.
x<-c(10, 10, 12, 13, 15, 16 ,17, 17, 17)
y<-c(112, 117, 111, 117, 114, 113, 119, 121, 130)
df <- data.frame(x,y)
aggregate(df, list(df$x), min)[,c("x","y")]
x y
1 10 112
2 12 111
3 13 117
4 15 114
5 16 113
6 17 119
> TIA,
> Bret
> R 2.6.0; platform i386-pc-mingw32
>
> ______________________________________________
> 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.
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