3

So, I have a large data frame(7000 rows) which is arranged like so:

head(mx)

                        Stem Progenitor   Astrocyte   Neuron genename
ENSRNOG00000000007 0.0517698   0.700234  0.11753300 4.591050     Gad1
ENSRNOG00000000010 0.0536043   0.471518  0.00741803 2.280760    Cbln1
ENSRNOG00000000012 0.0163017   0.285178  1.89533000 0.268405    Tcf15
ENSRNOG00000000024 2.7904200   0.703727 13.96940000 4.944650    HEBP1
ENSRNOG00000000028 2.5059900   2.563040  4.83952000 0.840013     Nde1
ENSRNOG00000000029 1.6204500   2.928300 15.58360000 1.750350    Myh11

I need to sort this data frame such that it is ordered from high to low by any value in the first four columns. So, for the example, the sorting for these 5 lines would be:

                        Stem Progenitor   Astrocyte   Neuron genename
ENSRNOG00000000029 1.6204500   2.928300 15.58360000 1.750350    Myh11
ENSRNOG00000000024 2.7904200   0.703727 13.96940000 4.944650    HEBP1
ENSRNOG00000000028 2.5059900   2.563040  4.83952000 0.840013     Nde1
ENSRNOG00000000007 0.0517698   0.700234  0.11753300 4.591050     Gad1
ENSRNOG00000000010 0.0536043   0.471518  0.00741803 2.280760    Cbln1
ENSRNOG00000000012 0.0163017   0.285178  1.89533000 0.268405    Tcf15

I know I can sort the data frame by one column at a time with a command such as:

mx <- mx[with(mx, order(-Stem, -Progenitor, -Astrocyte, -Neuron)),]

But, this would in the above example put Tcf15 above Gad1 and Cbln1. Is there a way to sort by the highest value in any of the four columns? I could write some script to do so by manually iterate through the data frame and sort into a new data frame using Rbind, but that's terribly inefficient and I'm sure there's a better way to do it.

thelatemail
  • 81,120
  • 12
  • 111
  • 172
biotinker
  • 51
  • 4
  • possible duplicate of [How to sort a dataframe by column(s) in R](http://stackoverflow.com/questions/1296646/how-to-sort-a-dataframe-by-columns-in-r) – Thomas Aug 02 '14 at 23:56

2 Answers2

6

Order the maximum of the four columns using pmax

mx <- mx[with(mx, order(-pmax(Stem, Progenitor, Astrocyte, Neuron))),]
Joshua Ulrich
  • 163,034
  • 29
  • 321
  • 400
2

With dplyr, this is:

library(dplyr)
arrange(ms, desc(pmax(Stem, Progenitor, Astrocyte, Neuron)))
hadley
  • 94,313
  • 27
  • 170
  • 239