As is typical with R there are often other ways. Here is another
approach that determines the rows of interest with tapply and min,
converts those minimums into logical "targets" with %in%, and extracts
them from "x" using indexing:
x[x$x2 %in% tapply(x$x2, x$x1, min), ]
########
x1 x2 x3
1 A 1 1.5
2 B 2 0.9
4 C 7 1.8
5 D 7 1.3
You might want to determine whether both would return all rows if
there were multiple instances of a minimum. I think the above solution
would return multiples while the one below would not. You choose based
on the nature of the problem.
--
David
On Oct 2, 2009, at 5:24 AM, jim holtman wrote:
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.
______________________________________________
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.