[R] split-apply question
hadley wickham
h.wickham at gmail.com
Fri Oct 2 15:07:15 CEST 2009
On Fri, Oct 2, 2009 at 4:24 AM, jim holtman <jholtman at 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), 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
>>
Or, using plyr and subset
library(plyr)
ddply(x, "x1", subset, x2 == min(x2))
Hadley
--
http://had.co.nz/
More information about the R-help
mailing list