Re: [R] split-apply question

2009-10-04 Thread Kavitha Venkatesan
t; > From: r-help-boun...@r-project.org > > [mailto:r-help-boun...@r-project.org] On Behalf Of hadley wickham > > Sent: Friday, October 02, 2009 6:07 AM > > To: jim holtman > > Cc: r-help@r-project.org; Kavitha Venkatesan > > Subject: Re: [R] split-apply quest

Re: [R] split-apply question

2009-10-02 Thread William Dunlap
> -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of hadley wickham > Sent: Friday, October 02, 2009 6:07 AM > To: jim holtman > Cc: r-help@r-project.org; Kavitha Venkatesan > Subject: Re: [R] split-apply ques

Re: [R] split-apply question

2009-10-02 Thread hadley wickham
On Fri, Oct 2, 2009 at 4: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(

Re: [R] split-apply question

2009-10-02 Thread Henrique Dallazuanna
You can use aggregate: aggregate(x[,c('x2','x3')], x['x1'], min) On Fri, Oct 2, 2009 at 12:43 AM, Kavitha Venkatesan 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

Re: [R] split-apply question

2009-10-02 Thread David Winsemius
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

Re: [R] split-apply question

2009-10-02 Thread jim holtman
try this: > x <- read.table(textConnection("x1 x2 x3 + A 11.5 + B 20.9 + B 32.7 + C 71.8 + D 71.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

Re: [R] split-apply question

2009-10-01 Thread andrew
?subset is probably what you want: subset(x, x1 == 'A') On Oct 2, 1:43 pm, Kavitha Venkatesan 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

[R] split-apply question

2009-10-01 Thread Kavitha Venkatesan
Hi, I have a data frame that looks like this: >x x1 x2 x3 A 11.5 B 20.9 B 32.7 C 71.8 D 71.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 on