Greetings, I hope this isn't obvious but I've read through the FAQ and the various other R/S books I have and I cannot, for the life of me, figure out how to sort a data frame by a specific variable. Suppose I have a four variable data.frame x and the variables are var1, var2, var3, var4. I want to sort on say var3 simply for display purposes. How can this be accomplished? Thanks in advance for any hints, RTFM (the exact one please), or any other useful pointers. BTW, I'm using version 1.4 on Linux-Mandrake FWIW. -- ________________________________________________________________________ Cheers, Patrick -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
On Fri, 4 Jan 2002, Patrick E. McKnight wrote:> Greetings, > > I hope this isn't obvious but I've read through the FAQ and the various > other R/S books I have and I cannot, for the life of me, figure out how > to sort a data frame by a specific variable. Suppose I have a four > variable data.frame x and the variables are var1, var2, var3, var4. I > want to sort on say var3 simply for display purposes. How can this be > accomplished? Thanks in advance for any hints, RTFM (the exact one > please), or any other useful pointers. >You want the order() function, which returns a permutation that can be used to reorder Eg, to plot points on a curve in increasing order so they can be connected with lines oo<-order(x) plot(x[oo],y[oo],type="l") -thomas -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
If df is your data frame then use: df[sort.list(df$var3),] "Patrick E. McKnight" To: r-help <r-help at lists.R-project.org> <pem at theriver.com> cc: Sent by: Subject: [R] sorting data frame owner-r-help at stat.ma th.ethz.ch 01/04/2002 01:52 PM Greetings, I hope this isn't obvious but I've read through the FAQ and the various other R/S books I have and I cannot, for the life of me, figure out how to sort a data frame by a specific variable. Suppose I have a four variable data.frame x and the variables are var1, var2, var3, var4. I want to sort on say var3 simply for display purposes. How can this be accomplished? Thanks in advance for any hints, RTFM (the exact one please), or any other useful pointers. BTW, I'm using version 1.4 on Linux-Mandrake FWIW. -- ________________________________________________________________________ Cheers, Patrick -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-. -.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._. _._._ -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._
x.sorted_x[order(x$var3),] if you want it in increasing order or x.sorted_x[rev(order(x$var3)),] if you want it in decreasing order On Fri, 4 Jan 2002, Patrick E. McKnight wrote:> Greetings, > > I hope this isn't obvious but I've read through the FAQ and the various > other R/S books I have and I cannot, for the life of me, figure out how > to sort a data frame by a specific variable. Suppose I have a four > variable data.frame x and the variables are var1, var2, var3, var4. I > want to sort on say var3 simply for display purposes. How can this be > accomplished? Thanks in advance for any hints, RTFM (the exact one > please), or any other useful pointers. > > BTW, I'm using version 1.4 on Linux-Mandrake FWIW. > > > -- > ________________________________________________________________________ > > Cheers, > > Patrick > > -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- > r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html > Send "info", "help", or "[un]subscribe" > (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch > _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._ >------------------------------------------------------------------------- claudia tebaldi NCAR RAP project scientist P.O. Box 3000 (303) 497-2830 Boulder, CO 80307 -------------------------------------------------------------------------- -.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.- r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html Send "info", "help", or "[un]subscribe" (in the "body", not the subject !) To: r-help-request at stat.math.ethz.ch _._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._