[R] select the last row by id group
Gabor Grothendieck
ggrothendieck at gmail.com
Mon Jun 12 20:43:23 CEST 2006
Try this:
score[tapply(rownames(score), score$id, tail, 1),]
On 6/12/06, Tony Chu <gatony at 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 at 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
>
More information about the R-help
mailing list