Re: [R] by-group processing

2009-05-08 Thread hadley wickham
On Wed, May 6, 2009 at 8:12 PM, jim holtman wrote: > Ths should do it: > >> do.call(rbind, lapply(split(x, x$ID), tail, 1)) >         ID Type N > 45900 45900    I 7 > 46550 46550    I 7 > 49270 49270    E 3 Or with plyr: library(plyr) ddply(x, "id", tail, 1) plyr encapsulates the common split-

Re: [R] by-group processing

2009-05-08 Thread Jun Shen
This is a problem much like the one I had a few weeks ago. David's solution is more concise. Try xtfrm() or rank() for factor sorting d=data[order(data$ID,-xtfrm(data$Type)),] should work. By the way I got it from Duncan. Jun On Fri, May 8, 2009 at 4:09 PM, David Freedman <3.14da...@gmail.com> w

Re: [R] by-group processing

2009-05-08 Thread David Freedman
sorry about the mistake - the -data$Type doesn't work: the '-' sign isn't valid for factors. I *thought* I had checked this before submitting a response ! HufferD wrote: > > On Thursday, May 07, 2009 7:45 PM, David Freedman wrote: > > > ...how about: > > d=data[order(data$ID,-data$Type),]

Re: [R] by-group processing

2009-05-08 Thread David Huffer
On Thursday, May 07, 2009 7:45 PM, David Freedman wrote: > ...how about: > d=data[order(data$ID,-data$Type),] > d[!duplicated(d$ID),] Does the "-data$Type" argument to the order function work? -- David   - David Huffer, Ph.D.

Re: [R] by-group processing

2009-05-07 Thread David Freedman
how about: d=data[order(data$ID,-data$Type),] d[!duplicated(d$ID),] Max Webber wrote: > > Given a dataframe like > > > data > ID Type N > 1 45900A 1 > 2 45900B 2 > 3 45900C 3 > 4 45900D 4 > 5 45900E 5 > 6 45900F 6 > 7 45900I 7 > 8

Re: [R] by-group processing

2009-05-07 Thread William Dunlap
ill Dunlap TIBCO Software Inc - Spotfire Division wdunlap tibco.com > -Original Message- > From: r-help-boun...@r-project.org > [mailto:r-help-boun...@r-project.org] On Behalf Of Max Webber > Sent: Wednesday, May 06, 2009 3:09 PM > To: r-help@r-project.org > Subject: [R

Re: [R] by-group processing

2009-05-06 Thread jim holtman
Ths should do it: > do.call(rbind, lapply(split(x, x$ID), tail, 1)) ID Type N 45900 45900I 7 46550 46550I 7 49270 49270E 3 On Wed, May 6, 2009 at 6:09 PM, Max Webber wrote: > Given a dataframe like > > > data >ID Type N > 1 45900A 1 > 2 45900B 2 > 3

Re: [R] by-group processing

2009-05-06 Thread Jorge Ivan Velez
Dear Max, By using "d" instead of "data" for your data set, here is one way: # First order the data by ID d <- with(d, d[order(ID),] ) # Then use tapply to get the indexes for the maximum values d[cumsum(with(d, tapply(N, ID, which.max))),] # ID Type N # 7 45900I 7 # 24 46550I 7 # 10 492

[R] by-group processing

2009-05-06 Thread Max Webber
Given a dataframe like > data ID Type N 1 45900A 1 2 45900B 2 3 45900C 3 4 45900D 4 5 45900E 5 6 45900F 6 7 45900I 7 8 49270A 1 9 49270B 2 10 49270E 3 18 46550A 1 19 46550B 2 20 46550C 3 21 46550