Thanks Peter, that did the trick. I'll modify my function so that the numeric conversion is done automatically thus saving me the extra step of converting later on.
Aaron Wells > Subject: RE: [R] ordering > Date: Wed, 11 Mar 2009 08:41:50 +1300 > From: palsp...@hortresearch.co.nz > To: awell...@hotmail.com; r-help@r-project.org > > Kia ora Aaron > > As you have identified, test[,2] is not numeric - it is probably factor. > Your function must have made the conversion, so you may want to modify > that. Alternative, try: > > test[order(as.numeric(as.character(test[,2]))),] > > BTW, str(test) is a good way to find out more about the structure of > your object. > > HTH .... > > Peter Alspach > > > > > > -----Original Message----- > > From: r-help-boun...@r-project.org > > [mailto:r-help-boun...@r-project.org] On Behalf Of aaron wells > > Sent: Wednesday, 11 March 2009 8:30 a.m. > > To: r-help@r-project.org > > Subject: [R] ordering > > > > > > 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@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. > > > > The contents of this e-mail are confidential and may be subject to legal > privilege. > If you are not the intended recipient you must not use, disseminate, > distribute or > reproduce all or any part of this e-mail or attachments. If you have received > this > e-mail in error, please notify the sender and delete all material pertaining > to this > e-mail. Any opinion or views expressed in this e-mail are those of the > individual > sender and may not represent those of The New Zealand Institute for Plant and > Food Research Limited. _________________________________________________________________ [[alternative HTML version deleted]] ______________________________________________ R-help@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.