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

Peter Dalgaard p.dalgaard at biostat.ku.dk
Fri Jul 8 02:21:15 CEST 2005


"Briggs, Meredith M" <Meredith.Briggs at 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 at biostat.ku.dk)                  FAX: (+45) 35327907




More information about the R-help mailing list