[R] Sorting of columns of a matrix

arun smartpink111 at yahoo.com
Wed Aug 29 17:56:40 CEST 2012


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.





More information about the R-help mailing list