Tim Van den Bulcke
2010-May-18 08:58 UTC
[R] how to select rows per subset in a data frame that are max. w.r.t. a column
Hi, I'd like to select one row in a data frame per subset which is maximal for a particular value. I'm pretty close to the solution in the sense that I can easily select the maximal values per subset using "aggregate", but I can't really figure out how to select the rows in the original data frame that are associated with these maximal values. library(stats) # this returns the list with maximal values for breaks per wool/tension subset maxValues = aggregate(warpbreaks$breaks, list(wool = wool, tension tension), max) # now i'd like the subset of the rows in warpbreaks that are associated with these maximal values Thank you in advance! Tim. [[alternative HTML version deleted]]
Ivan Calandra
2010-May-18 09:12 UTC
[R] how to select rows per subset in a data frame that are max. w.r.t. a column
Hi, Maybe it's just me but I don't understand what you're trying to do. Isn't maxValues what you need? Providing a reproducible example with your data (using the function dput), the desired output, and the code you've tried would really help! Ivan Le 5/18/2010 10:58, Tim Van den Bulcke a ?crit :> Hi, > > > I'd like to select one row in a data frame per subset which is maximal for a > particular value. I'm pretty close to the solution in the sense that I can > easily select the maximal values per subset using "aggregate", but I can't > really figure out how to select the rows in the original data frame that are > associated with these maximal values. > > > library(stats) > # this returns the list with maximal values for breaks per wool/tension > subset > maxValues = aggregate(warpbreaks$breaks, list(wool = wool, tension > tension), max) > > # now i'd like the subset of the rows in warpbreaks that are associated with > these maximal values > > > > Thank you in advance! > > Tim. > > [[alternative HTML version deleted]] > > ______________________________________________ > 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. > >-- Ivan CALANDRA PhD Student University of Hamburg Biozentrum Grindel und Zoologisches Museum Abt. S?ugetiere Martin-Luther-King-Platz 3 D-20146 Hamburg, GERMANY +49(0)40 42838 6231 ivan.calandra at uni-hamburg.de ********** http://www.for771.uni-bonn.de http://webapp5.rrz.uni-hamburg.de/mammals/eng/mitarbeiter.php
Dennis Murphy
2010-May-18 11:29 UTC
[R] how to select rows per subset in a data frame that are max. w.r.t. a column
Hi: Here are a couple of ways using the doBy and plyr packages: library(doBy) library(plyr) # doBy: subsetBy(~ wool + tension, subset = breaks == max(breaks), data warpbreaks) breaks wool tension A|H 43 A H A|L 70 A L A|M 36 A M B|H 28 B H B|L 44 B L B|M 42 B M # plyr ddply(warpbreaks, .(wool, tension), subset, breaks == max(breaks)) breaks wool tension 1 70 A L 2 36 A M 3 43 A H 4 44 B L 5 42 B M 6 28 B H HTH, Dennis On Tue, May 18, 2010 at 1:58 AM, Tim Van den Bulcke < vandenbulcketim@gmail.com> wrote:> Hi, > > > I'd like to select one row in a data frame per subset which is maximal for > a > particular value. I'm pretty close to the solution in the sense that I can > easily select the maximal values per subset using "aggregate", but I can't > really figure out how to select the rows in the original data frame that > are > associated with these maximal values. > > > library(stats) > # this returns the list with maximal values for breaks per wool/tension > subset > maxValues = aggregate(warpbreaks$breaks, list(wool = wool, tension > tension), max) > > # now i'd like the subset of the rows in warpbreaks that are associated > with > these maximal values > > > > Thank you in advance! > > Tim. > > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@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. >[[alternative HTML version deleted]]