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

From: Dimitrios Rizopoulos <Dimitris.Rizopoulos_at_med.kuleuven.be>
Date: Tue 13 Jun 2006 - 04:34:57 EST

one approach is the following:

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)
#############
ind <- cumsum(tapply(score$id, score$id, length)) score[ind, ]

I hope it helps.

Best,
Dimitris



Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium

Tel: +32/(0)16/336899
Fax: +32/(0)16/337015
Web: http://med.kuleuven.be/biostat/
     http://www.student.kuleuven.be/~m0390867/dimitris.htm


Quoting Tony Chu <gatony@gmail.com>:

> 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
>
>

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm



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:39:25 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.