Reputation: 51
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.
Upvotes: 3
Views: 3707
Reputation: 103948
With dplyr, this is:
library(dplyr)
arrange(ms, desc(pmax(Stem, Progenitor, Astrocyte, Neuron)))
Upvotes: 2
Reputation: 176698
Order the maximum of the four columns using pmax
mx <- mx[with(mx, order(-pmax(Stem, Progenitor, Astrocyte, Neuron))),]
Upvotes: 6