Re: [R] select the last row by id group

From: Gabor Grothendieck <ggrothendieck_at_gmail.com>
Date: Tue 13 Jun 2006 - 04:43:23 EST

Try this:

score[tapply(rownames(score), score$id, tail, 1),]

On 6/12/06, Tony Chu <gatony@gmail.com> wrote:
> Dear R users:
>
> I have a small test dataframe as the follows :
>
> math = c(80,75,70,65,65,70)
> reading = c(65,70,88,NA,90,NA)
> id = c('001','001','001','002','003','003')
> score = data.frame(id, reading, math)
>
> > score
> id reading math
> 1 001 65 80
> 2 001 70 75
> 3 001 88 70
> 4 002 NA 65
> 5 003 90 65
> 6 003 NA 70
>
> Could someone advise me tips about how to select the last row from
> each id group of 001, 002, & 003?
>
> In other words, the rows I need are :
>
> id reading math
> 3 001 88 70
> 4 002 NA 65
> 6 003 NA 70
>
> I tried function sebset but could not go very far. Thanks !
>
> ______________________________________________
> R-help@stat.math.ethz.ch mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html
>



R-help@stat.math.ethz.ch mailing list
https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide! http://www.R-project.org/posting-guide.html Received on Tue Jun 13 04:48:35 2006

Archive maintained by Robert King, hosted by the discipline of statistics at the University of Newcastle, Australia.
Archive generated by hypermail 2.1.8, at Tue 13 Jun 2006 - 06:12:23 EST.

Mailing list information is available at https://stat.ethz.ch/mailman/listinfo/r-help. Please read the posting guide before posting to the list.