Dear R-helpers, for the purpose of plotting a dataframe, i am trying to sort a dataframe by one column, for example tester <- data.frame(one=c(3,2,1), two=c(2,3,1)) #> tester # one two #1 3 2 #2 2 3 #3 1 1 # I want to sort "tester" by column "one", so that i get a dataframe # that looks like: #one two #1 1 #2 3 #3 2 I know of 'sort' but it can only sort vectors. Thanks for your help, Remko Duursma
> tester <- data.frame(one=c(3,2,1), two=c(2,3,1))> tester[order(tester$one),] one two 3 1 1 2 2 3 1 3 2 > Spencer Graves Remko Duursma wrote:> Dear R-helpers, > > for the purpose of plotting a dataframe, i am trying to sort a dataframe by one column, for example > > tester <- data.frame(one=c(3,2,1), two=c(2,3,1)) > > #> tester > # one two > #1 3 2 > #2 2 3 > #3 1 1 > > # I want to sort "tester" by column "one", so that i get a dataframe > # that looks like: > #one two > #1 1 > #2 3 > #3 2 > > I know of 'sort' but it can only sort vectors. > > Thanks for your help, > > Remko Duursma > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
Remko Duursma wrote:> for the purpose of plotting a dataframe, i am trying to sort a dataframe by one column, for example > > tester <- data.frame(one=c(3,2,1), two=c(2,3,1)) > > #> tester > # one two > #1 3 2 > #2 2 3 > #3 1 1 > > # I want to sort "tester" by column "one", so that i get a dataframe > # that looks like: > #one two > #1 1 > #2 3 > #3 2Remko: First examine the results of the following: order(tester[,1]) Then try this: tester[order(tester[,1]),] See ?order. hope it helps, Chuck Cleland
Dear Remko, Simply index the rows of the data frame by the order of elements in the first column: > tester[order(tester$one),] one two 3 1 1 2 2 3 1 3 2 > John At 10:51 AM 4/7/2003 -0700, Remko Duursma wrote:>for the purpose of plotting a dataframe, i am trying to sort a dataframe >by one column, for example > >tester <- data.frame(one=c(3,2,1), two=c(2,3,1)) > >#> tester ># one two >#1 3 2 >#2 2 3 >#3 1 1 > ># I want to sort "tester" by column "one", so that i get a dataframe ># that looks like: >#one two >#1 1 >#2 3 >#3 2 > >I know of 'sort' but it can only sort vectors.____________________________ John Fox Department of Sociology McMaster University email: jfox at mcmaster.ca web: http://www.socsci.mcmaster.ca/jfox
How does one sort a dataframe on multiple columns, say you first sort on variable 1 and then on variable 2, etc.? Is there a simple, one- liner to do this? thanks, Ravi. ----- Original Message ----- From: Spencer Graves <spencer.graves at pdf.com> Date: Monday, April 7, 2003 2:07 pm Subject: Re: [R] How to sort a dataframe?> > tester <- data.frame(one=c(3,2,1), two=c(2,3,1)) > > tester[order(tester$one),] > one two > 3 1 1 > 2 2 3 > 1 3 2 > > > Spencer Graves > > Remko Duursma wrote: > > Dear R-helpers, > > > > for the purpose of plotting a dataframe, i am trying to sort a > dataframe by one column, for example > > > > tester <- data.frame(one=c(3,2,1), two=c(2,3,1)) > > > > #> tester > > # one two > > #1 3 2 > > #2 2 3 > > #3 1 1 > > > > # I want to sort "tester" by column "one", so that i get a dataframe > > # that looks like: > > #one two > > #1 1 > > #2 3 > > #3 2 > > > > I know of 'sort' but it can only sort vectors. > > > > Thanks for your help, > > > > Remko Duursma > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >
mydata[order(mydata$col1, mydata$col2, mydata$col3, ...), ] HTH, Andy> -----Original Message----- > From: Ravi Varadhan [mailto:rvaradha at jhsph.edu] > Sent: Monday, April 07, 2003 6:22 PM > To: Spencer Graves > Cc: rhelp; den.duurs at lycos.com > Subject: Re: [R] How to sort a dataframe? > > > > How does one sort a dataframe on multiple columns, say you first sort > on variable 1 and then on variable 2, etc.? Is there a simple, one- > liner to do this? > > thanks, > Ravi. > > ----- Original Message ----- > From: Spencer Graves <spencer.graves at pdf.com> > Date: Monday, April 7, 2003 2:07 pm > Subject: Re: [R] How to sort a dataframe? > > > > tester <- data.frame(one=c(3,2,1), two=c(2,3,1)) > > > tester[order(tester$one),] > > one two > > 3 1 1 > > 2 2 3 > > 1 3 2 > > > > > Spencer Graves > > > > Remko Duursma wrote: > > > Dear R-helpers, > > > > > > for the purpose of plotting a dataframe, i am trying to sort a > > dataframe by one column, for example > > > > > > tester <- data.frame(one=c(3,2,1), two=c(2,3,1)) > > > > > > #> tester > > > # one two > > > #1 3 2 > > > #2 2 3 > > > #3 1 1 > > > > > > # I want to sort "tester" by column "one", so that i get > a dataframe > > > # that looks like: > > > #one two > > > #1 1 > > > #2 3 > > > #3 2 > > > > > > I know of 'sort' but it can only sort vectors. > > > > > > Thanks for your help, > > > > > > Remko Duursma > > > > > > ______________________________________________ > > > R-help at stat.math.ethz.ch mailing list > > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > > ______________________________________________ > > R-help at stat.math.ethz.ch mailing list > > https://www.stat.math.ethz.ch/mailman/listinfo/r-help > > > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help >------------------------------------------------------------------------------