Skip to content

Sorting a dataframe by one column?

2 messages · Mark, Chuck Cleland

#
Given the following dataframe:

A=1:10
B=(a-5)^2
DATAFRAME=data.frame(A,B)

How can I sort DATAFRAME increasing (or decreasing, for that matter)
by B without making reference to A, or any other column?

I've read ?order and ?sort but cannot seem to figure this out.

Thank you.

Mark
#
mtb954 mtb954 wrote:
You should have tried RSiteSearch("sort dataframe") also, since there 
are many examples in the archives of R-help.

  A=1:10
  B=(A-5)^2
  mydata=data.frame(A,B)
  mydata[order(mydata$B),]
  mydata[rev(order(mydata$B)),]