Skip to content

Sorting of columns of a matrix

8 messages · John Kane, Berend Hasselman, Nico Met +2 more

#
Perhaps you could supply the matrix using dput() ?  It is a handy way to supply sample data.  Just do dput(mydata), copy the results and paste into email.

At the moment your matrix is almost unreadable.

John Kane
Kingston ON Canada
____________________________________________________________
FREE ONLINE PHOTOSHARING - Share your photos online with your friends and family!
Visit http://www.inbox.com/photosharing to find out more!
#
On 29-08-2012, at 16:08, Nico Met wrote:

            
This is just as unreadable as your first try.
Please do as suggested

dput(X)

and also include a set.seed() before generating the matrix to make it reproducible.

Berend
#
Hi
<snip>
<snip>
I am not sure what do you want to do with it? Remove rows which has 0

X[X==0]<-NA
X[complete.cases(X),]

remove zeroes and sort values independently in each column?

X[X==0]<-NA
apply(X, 2, sort, decreasing=TRUE)

results in list as you have various number of values in columns.

Regards
Petr
#
HI,
Not sure I understand the last part.
Try this:
dat1<-structure(c(0.585528817843856, 0.709466017509524, 0, -0.453497173462763,
0.605887455840393, -1.81795596770373, 0.630098551068391,
-0.276184105225216,
-0.284159743943371, -0.919322002474128, -0.116247806352002,
1.81731204370422,
0.370627864257954, 0.520216457554957, -0.750531994502331,
0.816899839520583,
-0.886357521243213, -0.331577589942552, 1.12071265166956,
0.298723699267293,
0.779621924555324, 0, -0.644328429231302, -1.55313740522969,
-1.59770951669631, 1.80509751881082, -0.481647363694637, 0.620379801298422,
0.612123492650849, -0.162310976918126, 0.81187317855386, 2.19683354634753,
2.04919033740619, 1.63244563948047, 0.254271192814055, 0.491188279272559,
-0.324086578737118, -1.66205024385863, 1.76773385087297, 0,
1.12851083359018,
-2.38035806139704, -1.06026555215253, 0.937140540182908, 0.854451720330554,
1.46072940310409, -1.4130987778892, 0.567403253424482, 0.583187653435685,
-1.30679883346442), .Dim = c(5L, 10L))
?colnames(dat1)<-paste0("x",1:10)


apply(dat1,2,sort,decreasing=TRUE)
???????????? x1???????? x2???????? x3???????? x4???????? x5???????? x6
#[1,]? 0.7094660? 0.6300986? 1.8173120? 1.1207127? 0.7796219? 1.8050975
#[2,]? 0.6058875 -0.2761841? 0.5202165? 0.8168998? 0.0000000? 0.6203798
#[3,]? 0.5855288 -0.2841597? 0.3706279? 0.2987237 -0.6443284? 0.6121235
#[4,]? 0.0000000 -0.9193220 -0.1162478 -0.3315776 -1.5531374 -0.1623110
#[5,] -0.4534972 -1.8179560 -0.7505320 -0.8863575 -1.5977095 -0.4816474
??????????? x7???????? x8???????? x9??????? x10
#[1,] 2.1968335? 1.7677339? 1.1285108? 1.4607294
#[2,] 2.0491903? 0.4911883? 0.9371405? 0.5831877
#[3,] 1.6324456? 0.0000000? 0.8544517? 0.5674033
#[4,] 0.8118732 -0.3240866 -1.0602656 -1.3067988
#[5,] 0.2542712 -1.6620502 -2.3803581 -1.4130988


A.K.

----- Original Message -----
From: Nico Met <nicomet80 at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, August 29, 2012 9:24 AM
Subject: [R] Sorting of columns of a matrix

Dear all,

Please suggest me how can I do it.


I have a matrix which look like following:

? x1 x2 x3? t1 .01 0.3 0? t2 0 0.1 0.01? t3 0 .01 .01? t4 0 0? t5 5 0 0? t6
0 0 0? t7 0 0 0? t8 0 0 0? t9 0.6 0 0? t10 0 0 0.66? t11 0 0.6 0.11? t12 0
0.4 0

I want to sort decreasing order in each column based on rows. and then to
display only those rows which has a value. The expected out put matrix will
look like

? x1 x2 x3? t9 0.6 t11 0.6 t10 0.66? t1 .01 t12 0.4 t11 0.11?  t1 0.3 t2 .01
t2 0.1? ? t3 .01
many thanks

Nico

??? [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
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.
#
HI,
I guess this might be what you want.
dat1<-structure(c(0.585528817843856, 0.709466017509524, 0, -0.453497173462763,
0.605887455840393, -1.81795596770373, 0.630098551068391,
-0.276184105225216,
-0.284159743943371, -0.919322002474128, -0.116247806352002,
1.81731204370422,
0.370627864257954, 0.520216457554957, -0.750531994502331,
0.816899839520583,
-0.886357521243213, -0.331577589942552, 1.12071265166956,
0.298723699267293,
0.779621924555324, 0, -0.644328429231302, -1.55313740522969,
-1.59770951669631, 1.80509751881082, -0.481647363694637, 0.620379801298422,
0.612123492650849, -0.162310976918126, 0.81187317855386, 2.19683354634753,
2.04919033740619, 1.63244563948047, 0.254271192814055, 0.491188279272559,
-0.324086578737118, -1.66205024385863, 1.76773385087297, 0,
1.12851083359018,
-2.38035806139704, -1.06026555215253, 0.937140540182908, 0.854451720330554,
1.46072940310409, -1.4130987778892, 0.567403253424482, 0.583187653435685,
-1.30679883346442), .Dim = c(5L, 10L))
?colnames(dat1)<-paste0("x",1:10)
dat2<-apply(dat1,2,sort,decreasing=TRUE)
dat2[-which(rowSums(dat2==0)>0),]
? # ????????? x1???????? x2??????? x3???????? x4???????? x5???????? x6??????? x7
#[1,]? 0.7094660? 0.6300986? 1.817312? 1.1207127? 0.7796219? 1.8050975 2.1968335
#[2,] -0.4534972 -1.8179560 -0.750532 -0.8863575 -1.5977095 -0.4816474 0.2542712
? # ???????? x8??????? x9?????? x10
#[1,]? 1.767734? 1.128511? 1.460729
#[2,] -1.662050 -2.380358 -1.413099
A.K.



----- Original Message -----
From: Nico Met <nicomet80 at gmail.com>
To: r-help at r-project.org
Cc: 
Sent: Wednesday, August 29, 2012 9:24 AM
Subject: [R] Sorting of columns of a matrix

Dear all,

Please suggest me how can I do it.


I have a matrix which look like following:

? x1 x2 x3? t1 .01 0.3 0? t2 0 0.1 0.01? t3 0 .01 .01? t4 0 0? t5 5 0 0? t6
0 0 0? t7 0 0 0? t8 0 0 0? t9 0.6 0 0? t10 0 0 0.66? t11 0 0.6 0.11? t12 0
0.4 0

I want to sort decreasing order in each column based on rows. and then to
display only those rows which has a value. The expected out put matrix will
look like

? x1 x2 x3? t9 0.6 t11 0.6 t10 0.66? t1 .01 t12 0.4 t11 0.11?  t1 0.3 t2 .01
t2 0.1? ? t3 .01
many thanks

Nico

??? [[alternative HTML version deleted]]

______________________________________________
R-help at r-project.org mailing list
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.