Undoubtedly a simple question: I've looked at order() and sort() in the help pages for R1.7.1. It doesn't appear that these functions are immediately suited to doing the same thing as PROC SORT DATA = BLAH; BY X Y Z; RUN; in SAS. I have also checked Frank Harrell's Hmisc library. Could someone point me in the right direction so I can sort by the levels of Z within the levels of Y within the levels of X? Everything needs to be in ascending order. Much thanks in advance, david paul
On Tue, 2003-08-12 at 11:38, Paul, David A wrote:> Undoubtedly a simple question: > > I've looked at order() and sort() in the help pages for > R1.7.1. It doesn't appear that these functions are immediately > suited to doing the same thing as > > PROC SORT DATA = BLAH; > BY X Y Z; > RUN; > > in SAS. I have also checked Frank Harrell's Hmisc library. > Could someone point me in the right direction so I can sort > by the levels of Z within the levels of Y within the levels > of X? Everything needs to be in ascending order. > > > Much thanks in advance, > david paulA quick search of the R-help archive would reveal this recent post: https://www.stat.math.ethz.ch/pipermail/r-help/2003-July/034865.html :-) HTH, Marc Schwartz
Dear Paul, Use order() to get the indices of the ordered rows. See ?order> blah <- data.frame(X = rep(3:1, each=4), Y = rep(c(2,1,2,1), 3), Z = rep(2:1, 6)) > blahX Y Z 1 3 2 2 2 3 1 1 3 3 2 2 4 3 1 1 5 2 2 2 6 2 1 1 7 2 2 2 8 2 1 1 9 1 2 2 10 1 1 1 11 1 2 2 12 1 1 1> blah <- data.frame(X = rep(3:1, each=4), Y = rep(c(2,2,1,1), 3), Z = rep(2:1, 6)) > blahX Y Z 1 3 2 2 2 3 2 1 3 3 1 2 4 3 1 1 5 2 2 2 6 2 2 1 7 2 1 2 8 2 1 1 9 1 2 2 10 1 2 1 11 1 1 2 12 1 1 1> with(blah, order(X, Y, Z)) # this gives the indices of the ordered rows[1] 12 11 10 9 8 7 6 5 4 3 2 1> blah[with(blah, order(X, Y, Z)), ]X Y Z 12 1 1 1 11 1 1 2 10 1 2 1 9 1 2 2 8 2 1 1 7 2 1 2 6 2 2 1 5 2 2 2 4 3 1 1 3 3 1 2 2 3 2 1 1 3 2 2 HTH Thomas --- Thomas Hotz Research Associate in Medical Statistics University of Leicester United Kingdom Department of Epidemiology and Public Health 22-28 Princess Road West Leicester LE1 6TP Tel +44 116 252-5410 Fax +44 116 252-5423 Division of Medicine for the Elderly Department of Medicine The Glenfield Hospital Leicester LE3 9QP Tel +44 116 256-3643 Fax +44 116 232-2976> -----Original Message----- > From: Paul, David A [mailto:paulda at BATTELLE.ORG] > Sent: 12 August 2003 17:39 > To: 'r-help at stat.math.ethz.ch' > Subject: [R] Sorting a dataframe > > > Undoubtedly a simple question: > > I've looked at order() and sort() in the help pages for > R1.7.1. It doesn't appear that these functions are immediately > suited to doing the same thing as > > PROC SORT DATA = BLAH; > BY X Y Z; > RUN; > > in SAS. I have also checked Frank Harrell's Hmisc library. > Could someone point me in the right direction so I can sort > by the levels of Z within the levels of Y within the levels > of X? Everything needs to be in ascending order. > > > Much thanks in advance, > david paul > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
Why won't "order" solve your problem? "?order" in R 1.7.1 for Windows includes an example sorting lexicographically 3 variables in ascending order. spencer graves Paul, David A wrote:> Undoubtedly a simple question: > > I've looked at order() and sort() in the help pages for > R1.7.1. It doesn't appear that these functions are immediately > suited to doing the same thing as > > PROC SORT DATA = BLAH; > BY X Y Z; > RUN; > > in SAS. I have also checked Frank Harrell's Hmisc library. > Could someone point me in the right direction so I can sort > by the levels of Z within the levels of Y within the levels > of X? Everything needs to be in ascending order. > > > Much thanks in advance, > david paul > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help