Is there a R function for sort a data frame by a variable ? I know sort a vector, but I don't know sort a data frame by a column. Can you help me ? the sort() function don't work with data frame.
You could do something like this dataframe[order(dataframe$variable),] -----Original Message----- From: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] On Behalf Of Mario Morales Sent: Thursday, March 31, 2005 10:23 PM To: r-help at stat.math.ethz.ch Subject: [R] a R function for sort a data frame. Is there a R function for sort a data frame by a variable ? I know sort a vector, but I don't know sort a data frame by a column. Can you help me ? the sort() function don't work with data frame. ______________________________________________ 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
dfr <- data.frame(sample(1:50,10),sample(1:50,10)) colnames(dfr) <- c("a","b") dfr <- dfr[order(dfr$a),] dfr <- dfr[order(-dfr$a),] -----Original Message----- From: Mario Morales [mailto:malfonso at telecom.com.co] Sent: Thursday, March 31, 2005 10:23 PM To: r-help at stat.math.ethz.ch Subject: [R] a R function for sort a data frame. Is there a R function for sort a data frame by a variable ? I know sort a vector, but I don't know sort a data frame by a column. Can you help me ? the sort() function don't work with data frame. ______________________________________________ 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
look at "?order()", e.g., dat[order(dat[,1]),] will sort your data.frame wrt the first column. 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.ac.be/biostat/ http://www.student.kuleuven.ac.be/~m0390867/dimitris.htm ----- Original Message ----- From: "Mario Morales" <malfonso at telecom.com.co> To: <r-help at stat.math.ethz.ch> Sent: Friday, April 01, 2005 5:22 AM Subject: [R] a R function for sort a data frame.> Is there a R function for sort a data frame by a variable ? > > I know sort a vector, but I don't know sort a data frame by a > column. Can you help me ? > > the sort() function don't work with data frame. > > ______________________________________________ > 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 >
Hi, x <- data.frame(A=rnorm(10), B=round(runif(10,0,10)), C=rnorm(10)) #one way: #increasing order x.sort.in <- x[order(x$B),] x.sort.in #decreasing order: x.sort.de <- x[rev(order(x$B)),] x.sort.de Best, Matthias> -----Urspr?ngliche Nachricht----- > Von: r-help-bounces at stat.math.ethz.ch > [mailto:r-help-bounces at stat.math.ethz.ch] Im Auftrag von Mario Morales > Gesendet: Freitag, 01. April 2005 05:23 > An: r-help at stat.math.ethz.ch > Betreff: [R] a R function for sort a data frame. > > > Is there a R function for sort a data frame by a variable ? > > I know sort a vector, but I don't know sort a data frame by a > column. Can you help me ? > > the sort() function don't work with data frame. > > ______________________________________________ > 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 >
On Thu, 31 Mar 2005 22:22:32 -0500, Mario Morales <malfonso at telecom.com.co> wrote :>Is there a R function for sort a data frame by a variable ? > >I know sort a vector, but I don't know sort a data frame by a >column. Can you help me ? > >the sort() function don't work with data frame. >This is a FAQ, but the answer there looks a bit slim to me: 7.23 How can I sort the rows of a data frame? To sort the rows within a data frame, with respect to the values in one or more of the columns, simply use order(). Here's an example that sorts on two columns:> df <- data.frame(x = sample(1:2, 10, replace=T), y = rnorm(10)) > dfx y 1 1 1.50996670 2 2 -0.95740020 3 2 2.35863397 4 1 0.79743294 5 1 -1.75136964 6 2 -2.28762091 7 1 0.29517547 8 2 0.09726887 9 2 0.74852695 10 1 0.48862415> inds <- with(df, order(x, y)) > df[inds,]x y 5 1 -1.75136964 7 1 0.29517547 10 1 0.48862415 4 1 0.79743294 1 1 1.50996670 6 2 -2.28762091 2 2 -0.95740020 8 2 0.09726887 9 2 0.74852695 3 2 2.35863397
Le 01.04.2005 05:22, Mario Morales a ?crit :> Is there a R function for sort a data frame by a variable ? > > I know sort a vector, but I don't know sort a data frame by a > column. Can you help me ? > > the sort() function don't work with data frame. >take a look at ?order as in : A <- data.frame(x=c(1,6,3),y=c(2,4,9)) A[order(A$x),] -- Romain FRANCOIS : francoisromain at free.fr page web : http://addictedtor.free.fr/ (en construction) 06 18 39 14 69 / 01 46 80 65 60 _______________________________________________________ Etudiant en 3eme ann?e Institut de Statistique de l'Universit? de Paris (ISUP) Fili?re Industrie et Services http://www.isup.cicrp.jussieu.fr/
http://finzi.psych.upenn.edu/R/Rhelp02a/archive/39835.html (First hit using RSiteSearch("sort.data.frame") in R-2.1.0 alpha.) Andy> From: Mario Morales > > Is there a R function for sort a data frame by a variable ? > > I know sort a vector, but I don't know sort a data frame by a > column. Can you help me ? > > the sort() function don't work with data frame. > > ______________________________________________ > 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 > > >