Re: [S] Data Frame Subsetting Question

Bill Venables (wvenable@attunga.stats.adelaide.edu.au)
Fri, 29 May 1998 08:45:36 +0930


Allen Humbolt writes:
> 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?

I'm not sure how easy is easy. Here's one way.

> xdat <- dat[order(dat$Class, abs(dat$A - dat$B)), ]
> xdat <- xdat[c(1, diff(as.numeric(xdat$Class))) == 1, ]

Bill Venables.
-----------------------------------------------------------------------
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