[R] ordering

David Winsemius dwinsemius at comcast.net
Tue Mar 10 20:46:20 CET 2009


A) I predict that if you apply the str function to the second test  
that you will find that "con" is not numeric but rather of class  
character or factor. And the second test is probably not a matrix but  
rather a dataframe. Matrices in R need to have all their elements of  
the same class.

B) Read the FAQ ... find the one that tells you how to convert factors  
to numeric.

-- 
David Winsemius

On Mar 10, 2009, at 3:29 PM, aaron wells wrote:

>
> Hello, I would like to order a matrix by a specific column. For  
> instance:
>
>
>
>> test
>      [,1] [,2] [,3]
> [1,]    1  100   21
> [2,]    2    3   22
> [3,]    3  100   23
> [4,]    4   60   24
> [5,]    5   55   25
> [6,]    6   45   26
> [7,]    7   75   27
> [8,]    8   12   28
> [9,]    9   10   29
> [10,]   10   22   30
>>
>
> test[order(test[,2]),]
>      [,1] [,2] [,3]
> [1,]    2    3   22
> [2,]    9   10   29
> [3,]    8   12   28
> [4,]   10   22   30
> [5,]    6   45   26
> [6,]    5   55   25
> [7,]    4   60   24
> [8,]    7   75   27
> [9,]    1  100   21
> [10,]    3  100   23
>
>
> This works well and good in the above example matrix.  However in  
> the matrix that I actually want to sort (derived from a function  
> that I wrote) I get something like this:
>
>
>
>> test[order(as.numeric(test[,2])),]     ### First column is row.names
>
>
>         f con f.1 cov f.2 minimum f.3 maximum f.4   cl
> asahi    * 100   *   1   *     0.1   *       2   * test
> castet   * 100   *   2   *     0.1   *       5   * test
> clado    * 100   *   1   *     0.7   *       2   * test
> aulac    *  33   *   0   *     0.1   *     0.1   * test
> buell    *  33   *   0   *     0.1   *     0.1   * test
> camlas   *  33   *   0   *     0.1   *     0.1   * test
> carbig   *  33   *   1   *       1   *       1   * test
> poaarc   *  67   *   0   *     0.1   *     0.1   * test
> polviv   *  67   *   0   *     0.1   *     0.1   * test
>
>
>
>
> where R interprets 100 to be the lowest value and orders increasing  
> from there.
>
>
>
>> is.numeric(test[,2])
> [1] FALSE
>> is.double(test[,2])
> [1] FALSE
>> is.integer(test[,2])
> [1] FALSE
>> is.real(test[,2])
> [1] FALSE
>
>
>
>
> My questions are:  Why is this happening? and How do I fix  it?
>
>
>
> Thanks in advance!
>
>
>
>                              Aaron Wells
>
> _________________________________________________________________
>
>
> cns!503D1D86EBB2B53C!2285.entry?ocid=TXT_TAGLM_WL_UGC_Contacts_032009
> 	[[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.

David Winsemius, MD
Heritage Laboratories
West Hartford, CT




More information about the R-help mailing list