[R] Sorting of columns of a matrix

Berend Hasselman bhh at xs4all.nl
Wed Aug 29 16:16:31 CEST 2012


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




More information about the R-help mailing list