Something like the following should work:
last.n <- function(x, n) {
last <- nrow(x)
x[(last - n + 1):last, , drop=FALSE]
}
## Example: get the last two rows.
do.call(rbind, lapply(split(score, score$id), last.n, 2))
You might want to add a check in last.n() to make sure that there are at
least n rows to extract.
Andy
From: Lauri Nikkinen
>
> 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]]
>
> ______________________________________________
> [email protected] 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.
>
>
>
------------------------------------------------------------------------------
Notice: This e-mail message, together with any attachments,...{{dropped}}
______________________________________________
[email protected] 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.