try this: > x <- read.table(textConnection("x1 x2 x3 + A 1 1.5 + B 2 0.9 + B 3 2.7 + C 7 1.8 + D 7 1.3"), header=TRUE) > closeAllConnections() > do.call(rbind, lapply(split(seq(nrow(x)), x$x1), function(.row){ + x[.row[which.min(x$x2[.row])],] + })) x1 x2 x3 A A 1 1.5 B B 2 0.9 C C 7 1.8 D D 7 1.3 >
On Thu, Oct 1, 2009 at 11:43 PM, Kavitha Venkatesan <kavitha.venkate...@gmail.com> wrote: > Hi, > > I have a data frame that looks like this: > >>x > > x1 x2 x3 > A 1 1.5 > B 2 0.9 > B 3 2.7 > C 7 1.8 > D 7 1.3 > > I want to "group" by the x1 column and in the case of multiple x$x1 values > (e.g., "B")d, return rows that have the smallest values of x2. In the case > of rows with only one value of x1 (e.g., "A"), return the row as is. How can > I do that? For example, in the above case, the output I want would be: > > x1 x2 x3 > A 1 1.5 > B 2 0.9 > C 7 1.8 > D 7 1.3 > > > Thanks! > > [[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. > -- Jim Holtman Cincinnati, OH +1 513 646 9390 What is the problem that you are trying to solve? ______________________________________________ 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.