s-news
[Top] [All Lists]

Re: [S] Data Frame Subsetting Question

To: "Humbolt, Allen" <HumboltA@kochind.com>
Subject: Re: [S] Data Frame Subsetting Question
From: Bill Venables <wvenable@attunga.stats.adelaide.edu.au>
Date: Fri, 29 May 1998 08:45:36 +0930
Cc: s-news@wubios.wustl.edu
In-reply-to: <0DE9A7F084E7D1119FE100805F577E5517A35F@ICTMAIL07>
References: <0DE9A7F084E7D1119FE100805F577E5517A35F@ICTMAIL07>
Sender: owner-s-news@wubios.wustl.edu
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

<Prev in Thread] Current Thread [Next in Thread>