Re: [R] extracting the last row of each group in a data frame

2009-11-17 Thread Nutter, Benjamin
c(1, 2, 3, 4, 8, 2, 3)) > last.record(df, "Name", "Value") > last.record(df, 1, 2) -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-boun...@r-project.org] On Behalf Of Hao Cen Sent: Monday, November 16, 2009 2:43 PM To: r-help@r-project.org Subjec

Re: [R] extracting the last row of each group in a data frame

2009-11-16 Thread Hao Cen
Thanks to all who helped. These are all great suggestions. Jeff -Original Message- From: Gabor Grothendieck [mailto:ggrothendi...@gmail.com] Sent: Monday, November 16, 2009 6:27 PM To: Hao Cen Cc: r-help@r-project.org Subject: Re: [R] extracting the last row of each group in a data

Re: [R] extracting the last row of each group in a data frame

2009-11-16 Thread Gabor Grothendieck
Try this: > aggregate(DF[-1], DF[1], tail, 1) Name Value 1A 3 2B 8 3C 2 4D 3 On Mon, Nov 16, 2009 at 2:42 PM, Hao Cen wrote: > Hi, > > I would like to extract the last row of each group in a data frame. > > The data frame is as follows > > Name Value > A 1 > A

Re: [R] extracting the last row of each group in a data frame

2009-11-16 Thread Peter Ehlers
I would use pkg:plyr, but just to show how versatile R is: ind <- cumsum(rle(as.numeric(dat$Name))$lengths) dat[ind, ] where I'm assuming that your data frame is called 'dat'. -Peter Ehlers Hao Cen wrote: Hi, I would like to extract the last row of each group in a data frame. The data fram

Re: [R] extracting the last row of each group in a data frame

2009-11-16 Thread cls59
jeffc wrote: > > Hi, > > I would like to extract the last row of each group in a data frame. > > The data frame is as follows > > Name Value > A 1 > A 2 > A 3 > B 4 > B 8 > C 2 > D 3 > > I would like to get a data frame as > Name Value > A 3 > B 8 > C 2 > D 3 > > Thank you for your suggesti

Re: [R] extracting the last row of each group in a data frame

2009-11-16 Thread Jorge Ivan Velez
Dear Jeff, Here is a suggestion using tapply: data.frame(last = with(x, tapply(Value, Name, function(x) x[length(x)]))) See ?tapply for more information. HTH, Jorge On Mon, Nov 16, 2009 at 2:42 PM, Hao Cen <> wrote: > Hi, > > I would like to extract the last row of each group in a data frame

Re: [R] extracting the last row of each group in a data frame

2009-11-16 Thread David Winsemius
On Nov 16, 2009, at 2:42 PM, Hao Cen wrote: Hi, I would like to extract the last row of each group in a data frame. The data frame is as follows Name Value A 1 A 2 A 3 B 4 B 8 C 2 D 3 by(dfname$Value, dfname$Name, tail, 1) #which gets you a list Or: aggregate(dfname$Value, list(dfname$N

Re: [R] extracting the last row of each group in a data frame

2009-11-16 Thread baptiste auguie
Hi, You could try plyr, library(plyr) ddply(d,.(Name), tail,1) Name Value 1A 3 2B 8 3C 2 4D 3 HTH, baptiste 2009/11/16 Hao Cen : > Hi, > > I would like to extract the last row of each group in a data frame. > > The data frame is as follows > > Name Value > A

[R] extracting the last row of each group in a data frame

2009-11-16 Thread Hao Cen
Hi, I would like to extract the last row of each group in a data frame. The data frame is as follows Name Value A 1 A 2 A 3 B 4 B 8 C 2 D 3 I would like to get a data frame as Name Value A 3 B 8 C 2 D 3 Thank you for your suggestions in advance Jeff __