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]]
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
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]] > > ______________________________________________ > 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. > > >------------------------------------------------------------------------------ Notice: This e-mail message, together with any attachments,...{{dropped}}
On Tue, 2007-03-20 at 16:33 +0200, Lauri Nikkinen wrote:> 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, > LauriA slight modification to Gabor's solution:> scoreid reading math 1 1 65 80 2 1 70 75 3 1 88 70 4 2 NA 65 5 3 90 65 6 3 NA 70 # Return the last '2' rows # Note the addition of unlist()> score[unlist(tapply(rownames(score), score$id, tail, 2)), ]id reading math 2 1 70 75 3 1 88 70 4 2 NA 65 5 3 90 65 6 3 NA 70 Note that when tail() returns more than one value, tapply() will create a list rather than a vector:> tapply(rownames(score), score$id, tail, 2)$`1` [1] "2" "3" $`2` [1] "4" $`3` [1] "5" "6" Thus, we need to unlist() the indices to use them in the subsetting process that Gabor used in his solution. Another alternative, if the rownames do not correspond to the sequential row indices as they do in this example:> do.call("rbind", lapply(split(score, score$id), tail, 2))id reading math 1.2 1 70 75 1.3 1 88 70 2 2 NA 65 3.5 3 90 65 3.6 3 NA 70 This uses split() to create a list of data frames from score, where each data frame is 'split' by the 'id' column values. tail() is then applied to each data frame using lapply(), the results of which are then rbind()ed back to a single data frame. HTH, Marc Schwartz
Hi Lauri, here is a little modification of the solution for retrieving the last row only : score[as.vector(unlist(tapply(rownames(score), score$id, tail, 2))),] giving the last two rows. Replacing 2 by 6 or 10 gives you the last 6 or 10 rows (if they exist). Christian
You can try the following: n.len <- nrow(iris) n.sel <- 3 iris[(n.len-n.sel+1):n.len, ] -Christos> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Lauri Nikkinen > Sent: Tuesday, March 20, 2007 10:33 AM > To: r-help at stat.math.ethz.ch > 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. > >
Here is yet another solution. This one uses by() which generates nice visual output. score <- data.frame( id = c('001','001','001','002','003','003'), math = c(80,75,70,65,65,70), reading = c(65,70,88,NA,90,NA) ) out <- by( score, score$id, tail, n=2 ) # score$id: 001 # id math reading # 2 001 75 70 # 3 001 70 88 # ------------------------------------------------------------ # score$id: 002 # id math reading # 4 002 65 NA # ------------------------------------------------------------ # score$id: 003 # id math reading # 5 003 65 90 # 6 003 70 NA And if you want to put it back into a data frame, use do.call( "rbind", as.list(out) ) # id math reading # 001.2 001 75 70 # 001.3 001 70 88 # 002 002 65 NA # 003.5 003 65 90 # 003.6 003 70 NA Ignore the rownames here. HTH, Adai Lauri Nikkinen wrote:> 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. > > >