[R] Help on sorting

Richard Rowe Richard.Rowe at jcu.edu.au
Thu Sep 19 01:46:41 CEST 2002


At 15:14 18/09/02 -0700, you wrote:
>To: r-help-request at lists.R-project.org
>
>From: "Dr. Chris Wills" <cwills at ucsd.edu>
>
>Subject: Questions about sorting and functions
>Cc:
>
>Bcc:
>
>X-Attachments:
>Dear Gang -
>         Two questions for you:
>         1)  I cannot figure out how to sort one column in an array, and 
> drag the rest of the values that occupy the other columns along with the 
> sorted values in the first column.  What I would like to end up with is 
> an array that is the same size and dimensions as the first array.  When I 
> try various configurations of sort, I keep getting vectors instead of the 
> array I started with, and I have not found a way to make sort "bind" 
> other values to the sorted values.

does
B<-A[,c(order(A[,2]))]
do what you want?

A <- rnorm(30)
dim(A) <- c(5,6)
A
            [,1]        [,2]       [,3]       [,4]        [,5]       [,6]
[1,] -0.1916292 -2.10928866 -0.6838013  0.9762713 -0.14361845  0.8096648
[2,] -1.5046755  0.45943149 -0.6618188 -0.6555499 -0.51070702  0.9314748
[3,]  0.4615919  2.65414221  0.2993706  0.7574201  0.13423111  0.9506057
[4,] -0.2145235  0.07009774  0.4364953  0.8839637  0.08211918 -0.1553977
[5,]  0.2170495  1.59428139 -0.5239767  0.3468852  0.31420369 -0.8838015

B <- A[c(order(A[,2])),]
B
            [,1]        [,2]       [,3]       [,4]        [,5]       [,6]
[1,] -0.1916292 -2.10928866 -0.6838013  0.9762713 -0.14361845  0.8096648
[2,] -0.2145235  0.07009774  0.4364953  0.8839637  0.08211918 -0.1553977
[3,] -1.5046755  0.45943149 -0.6618188 -0.6555499 -0.51070702  0.9314748
[4,]  0.2170495  1.59428139 -0.5239767  0.3468852  0.31420369 -0.8838015
[5,]  0.4615919  2.65414221  0.2993706  0.7574201  0.13423111  0.9506057


Richard Rowe
Senior Lecturer
Department of Zoology and Tropical Ecology, James Cook University
Townsville, Queensland 4811, Australia
fax (61)7 47 25 1570
phone (61)7 47 81 4851
e-mail: Richard.Rowe at jcu.edu.au
http://www.jcu.edu.au/school/tbiol/zoology/homepage.html

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
r-help mailing list -- Read http://www.ci.tuwien.ac.at/~hornik/R/R-FAQ.html
Send "info", "help", or "[un]subscribe"
(in the "body", not the subject !)  To: r-help-request at stat.math.ethz.ch
_._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._._



More information about the R-help mailing list