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.namesf 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]]
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-bounces at r-project.org > [mailto:r-help-bounces at r-project.org] On Behalf Of aaron wells > Sent: Wednesday, 11 March 2009 8:30 a.m. > To: r-help at 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 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. >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.
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