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

2009-11-17 Thread Nutter, Benjamin
I usually use the following function: last.record - function(data, id, ..., na.last=TRUE, decreasing=FALSE){ #*** Make vector of variables to sort by v - c(id, unlist(list(...))) #*** Sort Data Frame data - data[do.call(order, c(data[,v, drop=FALSE], na.last=na.last,

[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

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 h...@andrew.cmu.edu: Hi, I would like to extract the last row of each group in a data frame. The data frame is as follows

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,

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 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 suggestions in advance Jeff

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

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 h...@andrew.cmu.edu wrote: Hi, I would like to extract the last row of each group in a data frame. The data frame is as follows Name

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