[R] Sort a data frame with respect to more than one variable

Dimitris Rizopoulos dimitris.rizopoulos at med.kuleuven.be
Wed Sep 21 11:06:51 CEST 2005

if you need nested sorting then you can still use order(), e.g., in a 
longitudinal setting you may want to sort your data.frame with respect 
to subject's id and time within subject then use,

df[order(df$id, df$time), ]

I hope it helps.


Dimitris Rizopoulos
Ph.D. Student
Biostatistical Centre
School of Public Health
Catholic University of Leuven

Address: Kapucijnenvoer 35, Leuven, Belgium
Tel: +32/16/336899
Fax: +32/16/337015
Web: http://www.med.kuleuven.be/biostat/

----- Original Message ----- 
From: "Marc Bernard" <bernarduse1 at yahoo.fr>
To: <r-help at stat.math.ethz.ch>
Sent: Wednesday, September 21, 2005 10:54 AM
Subject: [R] Sort a data frame with respect to more than one variable

> Dear All,
> How can I sort a data frame with respect to more than one  variable?
> I know that for one variable X  one may use:   df[order(df$X), ] 
> where df is  the data frame containing X.
> Many thanks,
> Bernard
> ---------------------------------
> [[alternative HTML version deleted]]
> ______________________________________________
> 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

Disclaimer: http://www.kuleuven.be/cwis/email_disclaimer.htm

More information about the R-help mailing list