[R] Is it possible to...

Prof Brian D Ripley ripley at stats.ox.ac.uk
Mon Apr 2 23:32:55 CEST 2001


On Mon, 2 Apr 2001, Marc Feldesman wrote:

> I have a matrix (1000 x 224) that contains the output from the function
> boot.array.  For each row, there are 224 columns that represent the case
> numbers of the selected individuals from a bootstrap.  The cases are listed
> in selection order, not numerical order.  I'd like to rearrange the cells
> so that every row has the column entries listed ascending numeric order.
>
> I've been able to do the sorting using two explicit for loops, but I cannot
> figure out a vectorized way to do it using apply, order, and/or sort.  Is
> there a clever vectorized way to do this?

Yes.  Do a lexicographic sort on row then value, of the whole matrix
as a vector.  There's an example in S Programming p. 153

Simpler:

A <- matrix(rnorm(1000*224), 1000, 224)
A1 <- t(apply(A, 1, sort))

as compared to

A2 <- t(matrix(A[order(row(A), A)], ,1000))

On my test the first was a little faster.

E&OE, of course.

-- 
Brian D. Ripley,                  ripley at stats.ox.ac.uk
Professor of Applied Statistics,  http://www.stats.ox.ac.uk/~ripley/
University of Oxford,             Tel:  +44 1865 272861 (self)
1 South Parks Road,                     +44 1865 272860 (secr)
Oxford OX1 3TG, UK                Fax:  +44 1865 272595

-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-.-
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