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

David Winsemius dwinsemius at comcast.net
Mon Nov 16 20:53:18 CET 2009


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$Name), tail, 1)  #which returns a  
data.frame
   Group.1 x
1       A 3
2       B 8
3       C 2
4       D 3

> I would like to get a data frame as
> Name Value
> A 3
> B 8
> C 2
> D 3
>

-- 

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list