[R] Sorting of columns of a matrix

PIKAL Petr petr.pikal at precheza.cz
Wed Aug 29 16:46:12 CEST 2012


Hi



> -----Original Message-----
> From: r-help-bounces at r-project.org [mailto:r-help-bounces at r-
> project.org] On Behalf Of Nico Met
> Sent: Wednesday, August 29, 2012 4:25 PM
> To: Berend Hasselman
> Cc: r-help at r-project.org
> Subject: Re: [R] Sorting of columns of a matrix
> 
> Please find the require info:
> 
> 
>  set.seed(12345)
>  X<-matrix(rnorm(5*10),nrow=5)
> 

<snip>

> 
>  X[2,5]<-0
>  X[3,1]<-0
>  X[5,8]<-0

<snip>

> 
> dput(X)
> 
> 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))
> 

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

> 
> On Wed, Aug 29, 2012 at 4:16 PM, Berend Hasselman <bhh at xs4all.nl>
> wrote:
> 
> >
> > On 29-08-2012, at 16:08, Nico Met wrote:
> >
> > > Hello john,
> > >
> > > thanks for the suggestion. Please find an example:
> > >
> > >> X<-matrix(rnorm(5*10),nrow=5)
> > >
> > >> dim(X)
> > > [1]  5 10
> > >> X
> > >           [,1]       [,2]        [,3]       [,4]       [,5]
> [,6]
> > >   [,7]       [,8]       [,9]      [,10]
> > > [1,]  1.2774431 -1.2427735  0.81933548 -2.1098586 -1.6726799
> > > -2.2994684
> > > -0.28823228  0.1929301  0.1187168 -1.2078372 [2,]  0.5969187
> > > -1.2571336  0.30292368 -0.2098139 -0.9689961  1.7105109
> > > 0.77240398  0.1482712 -1.2486152  0.2359723 [3,] -0.9669289
> > > -0.2315229  1.04611417  0.7902772  0.5791674  0.6296305
> > > -0.60517647  0.5317435 -0.2556417 -0.4838956 [4,] -1.9345400
> > > 0.2898647  0.08983352  0.1482130  0.8229054  0.7778114
> > > 0.49423641  0.4015327  2.8596603  0.6618432 [5,] -1.1969226
> > > -1.3991657 -0.76507258  0.5024922  0.2633307  0.0236774
> > > -0.09497448 -0.3986624  0.1327156 -0.8935923
> > >
> > >> X[2,5]<-0
> > >> X[3,1]<-0
> > >> X[6,8]<-0
> > >> X[5,8]<-0
> > >> X
> > >           [,1]       [,2]        [,3]       [,4]       [,5]
> [,6]
> > >   [,7]      [,8]       [,9]      [,10]
> > > [1,]  1.2774431 -1.2427735  0.81933548 -2.1098586 -1.6726799
> > > -2.2994684
> > > -0.28823228 0.1929301  0.1187168 -1.2078372 [2,]  0.5969187
> > > -1.2571336  0.30292368 -0.2098139  0.0000000  1.7105109
> > > 0.77240398 0.1482712 -1.2486152  0.2359723 [3,]  0.0000000
> > > -0.2315229  1.04611417  0.7902772  0.5791674  0.6296305
> > > -0.60517647 0.5317435 -0.2556417 -0.4838956 [4,] -1.9345400
> > > 0.2898647  0.08983352  0.1482130  0.8229054  0.7778114
> > > 0.49423641 0.4015327  2.8596603  0.6618432 [5,] -1.1969226
> > > -1.3991657 -0.76507258  0.5024922  0.2633307  0.0236774
> > > -0.09497448 0.0000000  0.1327156 -0.8935923
> >
> > 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
> >
> >
> 
> 	[[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