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 !
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 at 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 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
>
>
Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm
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 >