Re: [S] Data Frame Subsetting Question

Jan Schelling (schelling@ivuk.mavt.ethz.ch)
Fri, 29 May 1998 10:04:11 +0200


Try

x <- data.frame(Class=rep(c(1,2), c(4,3)), A=c(16:19, 17:19),
B=rep(c(17.43, 18.02), c(4,3)), Index=1:7)

do.call("rbind", tapply(1:nrow(x), x$Class, function(i)
x[i,][order(abs(x$A[i]-x$B[i]))[1],]))

-----------------------------------------------------------------
Jan Schelling
Institute of Process Engineering
ETH Zurich, 8092 Switzerland
phone: ++41-1-633 62 63
fax: ++41-1-633 11 19
email: schelling@ivuk.mavt.ethz.ch
internet: http://www.ivuk.ethz.ch/staff/schelling/
-----------------------------------------------------------------

At 16:52 28.05.98 -0500, Humbolt, Allen wrote:
>I have a data frame with data like the following.
>Class A B Index
> 1 16 17.43 1
> 1 17 17.43 2
> 1 18 17.43 3
> 1 19 17.43 4
> 2 17 18.02 5
> 2 18 18.02 6
> 2 19 18.02 7
>
>My goal is to select the subset of this data frame where A is closest to B
>within each class. My desired result for the above data would be the
>following.
>Class A B Index
> 1 17 17.43 2
> 2 18 18.02 6
>
>Any suggestions for an easy way to do this?
>
>Allen Humbolt
>Quantitative Analyst
>Koch Industries, Inc.
>HumboltA@kochind.com
>-----------------------------------------------------------------------
>This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
>send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
>message: unsubscribe s-news
>
-----------------------------------------------------------------------
This message was distributed by s-news@wubios.wustl.edu. To unsubscribe
send e-mail to s-news-request@wubios.wustl.edu with the BODY of the
message: unsubscribe s-news