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.

1

2 Answers 2

6

Order the maximum of the four columns using pmax

mx <- mx[with(mx, order(-pmax(Stem, Progenitor, Astrocyte, Neuron))),] 
Sign up to request clarification or add additional context in comments.

1 Comment

@Reed: I didn't read his data into an R session. My answer was from memory and untested.
2

With dplyr, this is:

library(dplyr) arrange(ms, desc(pmax(Stem, Progenitor, Astrocyte, Neuron))) 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.