[R] Selection of Records in a Data Frame Based on a Criteria Applying to Factors

Brian.J.GREGOR@odot.state.or.us Brian.J.GREGOR at odot.state.or.us
Wed Aug 28 02:46:22 CEST 2002

I was wondering whether there is a simpler way of selecting records from a
data frame based on criteria applied to records grouped by factors.  Each
record of my data frame represents a bridge.  The record includes data on
the bridge number (bridge.num), the number of the roadway link the bridge is
on (link.num), a restriction level for the bridge (status), and a year that
the restriction occurs (year).  A roadway link may have more than one bridge
on it.  What I need to do is make a data frame of roadway links where each
link record represents the greatest degree of bridge restriction present on
the link (e.g. the most restricted and the earliest year of restriction).
Here is how I process the bridges data frame to get my result:

bridges <- bridges[order(bridges$link.num),]
bridges <- bridges[unlist(tapply(bridges$status, bridges$link.id,
function(x) x==max(x))),]
bridges <- bridges[unlist(tapply(bridges$year, bridges$link.id, function(x)
bridges <- bridges[!duplicated(bridges$link.id),]  # remove remaining

Is there a better way to do this?

Brian Gregor, P.E.
Transportation Planning Analysis Unit
Oregon Department of Transportation
Brian.J.GREGOR at odot.state.or.us
(503) 986-4120
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch

More information about the R-help mailing list