?unique says Value: An object of the same type of 'x'. but if an element is equal to one with a smaller index, it is removed. However, I need to keep the one with the LARGEST index. Can someone please show me the light? I thought about reversing the row order twice, but I couldn't get it to work right (My data frame has 125000 rows and 7 columns, and I'm 'uniqueing' on column #1 (chron) only, although the class of the column may not matter.) Say, e.g.,> DF <- data.frame(t = c(1,2,3,1,4,5,1,2,3), x = c(0,1,2,3,4,5,6,7,8))I would like the result to be (sorted as well) t x 1 6 2 7 3 8 4 4 5 5 If I got the original rownames, that would be a bonus (for debugging.)> R.version_ platform i386-pc-mingw32 arch i386 os mingw32 system i386, mingw32 status major 2 minor 3.1 year 2006 month 06 day 01 svn rev 38247 language R version.string Version 2.3.1 (2006-06-01) Thanks for any hints! David David L. Reiner Rho Trading Securities, LLC Chicago? IL? 60605 312-362-4963
On Mon, 2006-07-24 at 12:00 -0500, davidr at rhotrading.com wrote:> ?unique says > > Value: > > An object of the same type of 'x'. but if an element is equal to > one with a smaller index, it is removed. > > However, I need to keep the one with the LARGEST index. > Can someone please show me the light? > I thought about reversing the row order twice, but I couldn't get it to work right > > (My data frame has 125000 rows and 7 columns, > and I'm 'uniqueing' on column #1 (chron) only, although the class of the column may not matter.) > > Say, e.g., > > DF <- data.frame(t = c(1,2,3,1,4,5,1,2,3), x = c(0,1,2,3,4,5,6,7,8)) > > I would like the result to be (sorted as well) > t x > 1 6 > 2 7 > 3 8 > 4 4 > 5 5 > > If I got the original rownames, that would be a bonus (for debugging.)Does this get it?> DF[sapply(unique(DF$t), function(x) max(which(DF$t == x))), ]t x 7 1 6 8 2 7 9 3 8 5 4 4 6 5 5 HTH, Marc Schwartz
Try: largestDF <- DF[nrow(DF)- which(!duplicated(rev(DF$t)))+1,] You can then sort this however you like in the usual way. Row names will be preserved. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > davidr at rhotrading.com > Sent: Monday, July 24, 2006 10:00 AM > To: r-help at stat.math.ethz.ch > Subject: [R] unique, but keep LAST occurence > > ?unique says > > Value: > > An object of the same type of 'x'. but if an element is equal to > one with a smaller index, it is removed. > > However, I need to keep the one with the LARGEST index. > Can someone please show me the light? > I thought about reversing the row order twice, but I couldn't > get it to work right > > (My data frame has 125000 rows and 7 columns, > and I'm 'uniqueing' on column #1 (chron) only, although the > class of the column may not matter.) > > Say, e.g., > > DF <- data.frame(t = c(1,2,3,1,4,5,1,2,3), x = c(0,1,2,3,4,5,6,7,8)) > > I would like the result to be (sorted as well) > t x > 1 6 > 2 7 > 3 8 > 4 4 > 5 5 > > If I got the original rownames, that would be a bonus (for debugging.) > > > R.version > _ > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 3.1 > year 2006 > month 06 > day 01 > svn rev 38247 > language R > version.string Version 2.3.1 (2006-06-01) > > Thanks for any hints! > David > > David L. Reiner > Rho Trading Securities, LLC > Chicago? IL? 60605 > 312-362-4963 > > ______________________________________________ > 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. >
Thank you, Bert and Mark. I believe Mark's solution works, but it was taking a very long time. Bert's is very fast. My day is saved! David L. Reiner Rho Trading Securities, LLC Chicago IL 60605 312-362-4963 -----Original Message----- From: Berton Gunter [mailto:gunter.berton at gene.com] Sent: Monday, July 24, 2006 12:51 PM To: David Reiner <davidr at rhotrading.com>; r-help at stat.math.ethz.ch Subject: RE: [R] unique, but keep LAST occurence Try: largestDF <- DF[nrow(DF)- which(!duplicated(rev(DF$t)))+1,] You can then sort this however you like in the usual way. Row names will be preserved. -- Bert Gunter Genentech Non-Clinical Statistics South San Francisco, CA "The business of the statistician is to catalyze the scientific learning process." - George E. P. Box> -----Original Message----- > From: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of > davidr at rhotrading.com > Sent: Monday, July 24, 2006 10:00 AM > To: r-help at stat.math.ethz.ch > Subject: [R] unique, but keep LAST occurence > > ?unique says > > Value: > > An object of the same type of 'x'. but if an element is equal to > one with a smaller index, it is removed. > > However, I need to keep the one with the LARGEST index. > Can someone please show me the light? > I thought about reversing the row order twice, but I couldn't > get it to work right > > (My data frame has 125000 rows and 7 columns, > and I'm 'uniqueing' on column #1 (chron) only, although the > class of the column may not matter.) > > Say, e.g., > > DF <- data.frame(t = c(1,2,3,1,4,5,1,2,3), x = c(0,1,2,3,4,5,6,7,8)) > > I would like the result to be (sorted as well) > t x > 1 6 > 2 7 > 3 8 > 4 4 > 5 5 > > If I got the original rownames, that would be a bonus (for debugging.) > > > R.version > _ > platform i386-pc-mingw32 > arch i386 > os mingw32 > system i386, mingw32 > status > major 2 > minor 3.1 > year 2006 > month 06 > day 01 > svn rev 38247 > language R > version.string Version 2.3.1 (2006-06-01) > > Thanks for any hints! > David > > David L. Reiner > Rho Trading Securities, LLC > Chicago? IL? 60605 > 312-362-4963 > > ______________________________________________ > 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. >