[R] Select the last two rows by id group

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Tue Mar 20 15:57:03 CET 2007


you can use the following (based on Gabor's answer):

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)

#############

# it might be useful to sort first
# score <- score[order(score$id), ]

score
score[unlist(tapply(row.names(score), score$id, tail, n = 2)), ]

look at ?tail() for more info.

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


----- Original Message ----- 
From: "Lauri Nikkinen" <lauri.nikkinen at iki.fi>
To: <r-help at stat.math.ethz.ch>
Sent: Tuesday, March 20, 2007 3:33 PM
Subject: [R] Select the last two rows by id group


> Hi R-users,
>
> Following this post 
> http://tolstoy.newcastle.edu.au/R/help/06/06/28965.html ,
> how do I get last two rows (or six or ten) by id group out of the 
> data
> frame? Here the example gives just the last row.
>
> Sincere thanks,
> Lauri
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> 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
> and provide commented, minimal, self-contained, reproducible code.
> 


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



More information about the R-help mailing list