-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 question
On Fri, Oct 2, 2009 at 4:24 AM, jim holtman
jholt
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 1
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
You can use aggregate:
aggregate(x[,c('x2','x3')], x['x1'], min)
On Fri, Oct 2, 2009 at 12:43 AM, 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
On Fri, Oct 2, 2009 at 4:24 AM, jim holtman jholt...@gmail.com 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),
-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 question
On Fri, Oct 2, 2009
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 one
?subset is probably what you want:
subset(x, x1 == 'A')
On Oct 2, 1: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
8 matches
Mail list logo