Marc Bernard
2005-Sep-21 08:54 UTC
[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]]
vincent@7d4.com
2005-Sep-21 09:02 UTC
[R] Sort a data frame with respect to more than one variable
Marc Bernard a ??crit :> I know that for one variable X one may use:> df[order(df$X), ] where df is the data frame containing X. probably not optimal, but simply why not ? dfX = df[order(df$X), ]; dfXY = dfX[order(dfX$Y), ]; hih
Romain Francois
2005-Sep-21 09:06 UTC
[R] Sort a data frame with respect to more than one variable
Le 21.09.2005 10:54, Marc Bernard a ??crit :>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 > >You already know the answer of that question. order() accepts more than one variable. x <- rep(rbinom(5,size=4,prob=.5), 6) y <- rnorm(30) cbind(x,y)[ order(x,y) , ] Romain -- visit the R Graph Gallery : http://addictedtor.free.fr/graphiques ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~ ~~~~~~ Romain FRANCOIS - http://addictedtor.free.fr ~~~~~~ ~~~~ Etudiant ISUP - CS3 - Industrie et Services ~~~~ ~~ http://www.isup.cicrp.jussieu.fr/ ~~ ~~~~ Stagiaire INRIA Futurs - Equipe SELECT ~~~~ ~~~~~~ http://www.inria.fr/recherche/equipes/select.fr.html ~~~~~~ ~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~
Dimitris Rizopoulos
2005-Sep-21 09:06 UTC
[R] Sort a data frame with respect to more than one variable
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.
Best,
Dimitris
----
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/
http://www.student.kuleuven.be/~m0390867/dimitris.htm
----- 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