 Hi

I'd like to add that in my experience using "order" would be the preferred
method as "sort" can lead to unexpected results as I hope the following
example will show.

> a <- matrix(4:1, 2,2, byrow=TRUE)
> colnames(a) <- c('b', 'a')
> a
     b a
[1,] 4 3
[2,] 2 1
> sort(a[2,])
a b 
1 2 
> # works as expected whereas the following can lead the unwary user astray

> sort(a[2,,drop=FALSE])
     b a
[1,] 1 2
> 

This led to some errors in my code which took me a while to track down so
I'd just like to put it out there as a caveat.

Not having looked at the source code of "sort" in any detail and going
purely on input->black box->output based experience it seems to me that
"sort" only sorts the associated "names" attribute and not the "colnames".
Now the "drop=FALSE" part in the above example might seem somewhat
artificial but when dealing with timeseries this occurs quite often.  For
example, carrying on with the above example

> library(zoo)
> t <- zoo(a, c(2001,2002))
> t
     b a
2001 4 3
2002 2 1
> t2 <- window(t, 2002)
> t2
     b a
2002 2 1
> sort(t2)
     b a
2002 1 2
> sort(as.vector(t2))
[1] 1 2
> sort(as.matrix(t2))
     b a
[1,] 1 2
> sort(as.matrix(t2)[1,])
a b 
1 2 
> sort(t2[1,])
     b a
2002 1 2
> # none of which really gives the desired result since there is always some
loss of information or erroneous
> # information, ie. loss of dimension names or incorrectly labelled
dimensions.
> # Using order on the other hand is transparent and preserves all the
information.
> t2[,order(t2)]
     a b
2002 1 2
> 

Regards

Tobias 


>-----Original Message-----
>From: r-sig-finance-bounces@stat.math.ethz.ch 
>[mailto:r-sig-finance-bounces@stat.math.ethz.ch] On Behalf Of 
>Rainer Böhme
>Sent: 16 November 2005 12:29 PM
>To: seanpor@acm.org
>Cc: r-sig-finance@stat.math.ethz.ch; L.Isella
>Subject: Re: [R-sig-finance] Sorting
>
>Hi Lorenzo,
>
># given this 2 x N matrix
>
>N <- 10^5
>A <- matrix(rnorm(2*N),2)
>
># use the following statement to sort the column vectors by 
>their elements in the i-th row
>
>i <- 1
>A <- A[,order(A[i,])]
>
>Hope this helps,
>Rainer
>
>> Good Morning Lorenzo,
>>
>> I had a look at ?sort and it said "For ordering along more than one 
>> variable, e.g., for sorting data frames, see 'order'." So I 
>had a look 
>> at ?order... and popped down to the examples and it talks about 
>> ordering a dataframe - not the clearest of examples, but hey :-) 
>> Sometimes the obvious is the hardest to see :-)
>>
>> cheers!
>> Sean
>>
>>
>> On 16/11/05, L.Isella <L.Isella@myrealbox.com> wrote:
>>> Dear All,
>>> I have a long array (say 2xN, with N of the order of 10^5 at least) 
>>> made up of couples of numerical values.
>>> I would like to sort these N couples in increasing order of 
>the value 
>>> of the numbers in the lower row.
>>> It is very easy to use sort() to take care of the sorting of the 
>>> lower row, but then I also have to sort the upper row so that the 
>>> values of each couple still match.
>>> I did it using a double loop, but for large N this is very slow.
>>> This is really a bottleneck in my code...
>>> Any suggestions?
>>> Regards
>>>
>>> Lorenzo
>>>
>>> _______________________________________________
>>> R-sig-finance@stat.math.ethz.ch mailing list 
>>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>>
>>
>> _______________________________________________
>> R-sig-finance@stat.math.ethz.ch mailing list 
>> https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>>
>
>_______________________________________________
>R-sig-finance@stat.math.ethz.ch mailing list 
>https://stat.ethz.ch/mailman/listinfo/r-sig-finance
>

********************
Nedbank Limited Reg No 1951/000009/06
Directors: WAM Clewlow (Chairman)  Prof MM Katz (Vice-chairman)  ML Ndlovu (Vice-chairman)  TA Boardman (Chief Executive)
CJW Ball  MWT Brown  RG Cottrell  BE Davison  N Dennis (British)  MA Enus-Brey  Prof B de L Figaji  RM Head (British)
RJ Khoza  JB Magwaza  ME Mkwanazi  JVF Roberts (British)  CML Savage  GT Serobe  JH Sutcliffe (British)
Company Secretary: GS Nienaber     16.08.2005

This email and any accompanying attachments may contain confidential and proprietary information.  This information is private and protected by law and, accordingly, if you are not the intended recipient, you are requested to delete this entire communication immediately and are notified that any disclosure, copying or distribution of or taking any action based on this information is prohibited.

Emails cannot be guaranteed to be secure or free of errors or viruses.  The sender does not accept any liability or responsibility for any interception, corruption, destruction, loss, late arrival or incompleteness of or tampering or interference with any of the information contained in this email or for its incorrect delivery or non-delivery for whatsoever reason or for its effect on any electronic device of the recipient.

If verification of this email or any attachment is required, please request a hard-copy version.
********************

	[[alternative HTML version deleted]]

