[R] sorting matrix by sets rows

Bert Gunter bgunter.4567 at gmail.com
Sun Feb 7 06:34:30 CET 2016


?order

is what I think you want. There is a slight wrinkle here, however. You
want to sort Cluster in *increasing* order and Byers_EMT in
*decreasing*, as I understand. order() will do both increasing, or
both decreasing, but not differently. So a simple but inefficient way
around this is to first do them both decreasing and then use order()
by Cluster increasing. Perhaps others will find a slicker way. Also
**PLEASE** note that your structure is a data frame, not a matrix! If
you do not understand the difference you need to spend some time with
an R tutorial or two to learn. It is basic and essential.

Anyway here is my suggestion. I have taken a slight shortcut by using
the very convenient with() function. See ?with for details.

## z is your data frame from dput. Thanks for the minimal example.

znew <- with(z,z[order(Cluster,Byers_EMT), ])

## now sort by Cluster

znew <- with(znew,znew[order(Cluster), ])


Again, while I think this does what you want, someone else may provide
something slicker. Or simpler. But the order() function is still very
useful to know about anyway for this sort of thing.

Cheers,
Bert
Bert Gunter

"The trouble with having an open mind is that people keep coming along
and sticking things into it."
-- Opus (aka Berkeley Breathed in his "Bloom County" comic strip )


On Sat, Feb 6, 2016 at 3:45 PM, Adrian Johnson
<oriolebaltimore at gmail.com> wrote:
> Hello:
> sorry I've been trying to sort a matrix to make waterfall plot using
> barplot function.
>
> I have have 30x5 matrix.    Rows are samples and columns are results
> for a test as numeric vector.
>
> I want sort matrix by column. For example, first I want to sort column
> 1, in decreasing values where column 5 $ Cluster == 1 and then again
> decreasing by those rows with column == 2.
>
> How can I sort the column, by decreasing order, first by samples where
> clusters belong to 1 and then sort rows that belong to cluster 2.
>
> Thanks
> adrian
>
> Example data is given as dput.
>
> structure(list(Byers_EMT = c(4003.034387, 3768.281515, 3050.928331,
> 3176.920101, 2934.668097, 4823.117405, 3223.478884, 4241.000063,
> 2283.048518, 4338.528845, 3036.77349, 4300.743191, 3368.661555,
> 4658.908373, 4388.761884, 4081.057216, 3096.255942, 4705.843311,
> 4198.967015, 4273.724545, 3748.975301, 3686.148902, 4538.225296,
> 3799.86772, 4055.619424, 2242.591587, 3240.442781, 4881.301143,
> 4715.630605, 2390.426857), GOTZ_EMT = c(-462.4488505, 221.7113983,
> 382.0156009, -213.9475246, 412.4785725, -876.3339338, -428.6239051,
> -904.5665305, 1095.115995, -74.39566533, 462.2938373, -1114.129494,
> 490.1939685, -686.8375546, -265.4837211, -19.85851263, -143.0651772,
> -474.8515908, 59.99735, -245.9500041, -116.9701906, 164.9975774,
> -353.3706953, 296.4978516, -225.2971721, 841.0098397, 311.3738267,
> -497.1874598, -507.8169759, 893.5205575), GOT_EMT = c(3647.970274,
> 4308.586674, 4284.509207, 3864.143199, 4546.207066, 2857.187171,
> 3689.57683, 3342.042018, 5081.829902, 3984.897048, 4300.881677,
> 3296.588856, 4187.054703, 3586.055372, 3614.984328, 4478.424137,
> 3725.619911, 3922.904098, 4055.279616, 3915.446721, 4087.219866,
> 4243.127381, 4029.672242, 4331.676433, 4034.186023, 4718.144867,
> 4627.10284, 3948.356452, 3856.944245, 5041.236254), GOT_DN_EMT = c(4110.419124,
> 4086.875276, 3902.493606, 4078.090724, 4133.728494, 3733.521105,
> 4118.200735, 4246.608548, 3986.713907, 4059.292713, 3838.587839,
> 4410.71835, 3696.860734, 4272.892927, 3880.468049, 4498.282649,
> 3868.685088, 4397.755689, 3995.282266, 4161.396725, 4204.190056,
> 4078.129803, 4383.042937, 4035.178581, 4259.483195, 3877.135027,
> 4315.729014, 4445.543912, 4364.761221, 4147.715697), Cluster = c(1L,
> 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
> 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L)), .Names = c("Byers_EMT",
> "GOTZ_EMT", "GOT_EMT", "GOT_DN_EMT", "Cluster"), class = "data.frame",
> row.names = c("BA.5555",
> "CN.5356", "CR.7398", "CR.7402", "CV.7247", "CN.6988", "CV.5440",
> "BA.6869", "CN.6022", "CN.5360", "CR.7399", "CV.7250", "CV.7440",
> "CV.7242", "CN.4739", "CV.7421", "CV.5441", "CR.7364", "CN.4727",
> "F7.7848", "CV.7245", "CR.7370", "CR.7371", "CV.7415", "CV.6935",
> "CV.5444", "CV.5978", "CN.4738", "CV.7089", "CN.6989"))
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list