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))
> blah
   X 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))
> blah
   X 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:[EMAIL PROTECTED]
> Sent: 12 August 2003 17:39
> To: '[EMAIL PROTECTED]'
> 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
> 
> ______________________________________________
> [EMAIL PROTECTED] mailing list
> https://www.stat.math.ethz.ch/mailman/listinfo/r-help
>

______________________________________________
[EMAIL PROTECTED] mailing list
https://www.stat.math.ethz.ch/mailman/listinfo/r-help

Reply via email to