Re: [R] How do you sort a data frame on a selection of columns?

From: Peter Dalgaard <p.dalgaard_at_biostat.ku.dk>
Date: Fri 08 Jul 2005 - 10:21:15 EST

"Briggs, Meredith M" <Meredith.Briggs@team.telstra.com> writes:

> This is what to start with:
>
> Data Frame A B C D
> c1 4 y 5
> c3 6 d 7
> c1 5 t 6
>
> Now sort on A then C
>
> This is what to end with:
>
> Data Frame A B C D
> c1 5 t 6
> c1 4 y 5
> c3 6 d 7
>
> I assume it is something like this:
>
> attach(DF)

Attaching data frames before modifying them is not usually a good idea. Especially if you forget to detach them again.

> sort(DF,partial=c(A,C))

o <- with(DF, order(A,C)) # or just order(DF$A, DF$C) DF <- DF[o,]

-- 
   O__  ---- Peter Dalgaard             ุster Farimagsgade 5, Entr.B
  c/ /'_ --- Dept. of Biostatistics     PO Box 2099, 1014 Cph. K
 (*) \(*) -- University of Copenhagen   Denmark          Ph:  (+45) 35327918
~~~~~~~~~~ - (p.dalgaard@biostat.ku.dk)                  FAX: (+45) 35327907

______________________________________________
R-help@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
Received on Fri Jul 08 10:28:34 2005

This archive was generated by hypermail 2.1.8 : Fri 03 Mar 2006 - 03:33:22 EST