Skip to content
Back to formatted view

Raw Message

Message-ID: <4B01B330.40306@ucalgary.ca>
Date: 2009-11-16T20:16:48Z
From: Peter Ehlers
Subject: extracting the last row of each group in a data frame
In-Reply-To: <6e9bb1ed91068f2ef02f2b705c0fd05f.squirrel@webmail.andrew.cmu.edu>

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 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
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
>