[R] How to sort a dataframe?

John Fox jfox at mcmaster.ca
Mon Apr 7 20:15:36 CEST 2003


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



More information about the R-help mailing list