Hello, I'm somewhat new to R. I've searched the archive for the last year and tried to consult the manual pages for the following problem, but did not find an answer. I want to sort an array by the index values. Here is the array>acc.gp.bl.wtmnGp17 Gp4 1 0.5703125 0.6406250 10 0.7812500 0.7109375 11 0.8046875 0.7343750 12 0.8359375 0.7890625 13 0.8515625 0.7109375 14 0.8281250 0.7343750 15 0.8671875 0.7812500 16 0.8125000 0.7578125 17 0.7734375 0.7500000 18 0.8000000 0.7800000 19 0.8700000 0.7300000 2 0.8046875 0.7265625 20 0.8300000 0.7400000 21 0.7500000 0.8000000 22 0.7700000 0.7600000 23 0.8200000 0.7800000 24 0.7600000 0.7400000 25 0.8100000 0.8000000 3 0.7578125 0.7812500 4 0.8203125 0.7890625 5 0.7890625 0.7421875 6 0.7265625 0.7812500 7 0.8125000 0.7421875 8 0.8515625 0.7265625 9 0.8203125 0.6953125 My question is: How do I sort by the first index (the values are 1:25) without losing the associated values per index value for the two columns(Gp17 and Gp4). I want ascending order (1:25) of the index values. Thank you in advance. -Mark Orr Postdoctoral Fellow Psychology Dept. Carnegie Mellon Univ. Pittsburgh, PA 15213
acc.gp.bl.wtmn[sort.list(row.names(acc.gp.bl.wtmn)), ] I am not clear how you missed order/sort.list, which have examples like this. It's referenced off ?sort. Indeed, sort() can be used too. On Tue, 4 Feb 2003, Mark G Orr wrote:> Hello, I'm somewhat new to R. I've searched the archive for the last year > and tried to consult the manual pages for the following problem, but did > not find an answer. > > I want to sort an array by the index values. Here is the array > > >acc.gp.bl.wtmn > Gp17 Gp4 > 1 0.5703125 0.6406250 > 10 0.7812500 0.7109375 > 11 0.8046875 0.7343750 > 12 0.8359375 0.7890625 > 13 0.8515625 0.7109375 > 14 0.8281250 0.7343750 > 15 0.8671875 0.7812500 > 16 0.8125000 0.7578125 > 17 0.7734375 0.7500000 > 18 0.8000000 0.7800000 > 19 0.8700000 0.7300000 > 2 0.8046875 0.7265625 > 20 0.8300000 0.7400000 > 21 0.7500000 0.8000000 > 22 0.7700000 0.7600000 > 23 0.8200000 0.7800000 > 24 0.7600000 0.7400000 > 25 0.8100000 0.8000000 > 3 0.7578125 0.7812500 > 4 0.8203125 0.7890625 > 5 0.7890625 0.7421875 > 6 0.7265625 0.7812500 > 7 0.8125000 0.7421875 > 8 0.8515625 0.7265625 > 9 0.8203125 0.6953125 > > My question is: How do I sort by the first index (the values are 1:25) > without losing the associated values per index value for the two > columns(Gp17 and Gp4). I want ascending order (1:25) of the index values.I think the `first column' is the row names. -- 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 272866 (PA) Oxford OX1 3TG, UK Fax: +44 1865 272595
On Tue, 4 Feb 2003, Mark G Orr wrote:> My question is: How do I sort by the first index (the values are 1:25) > without losing the associated values per index value for the two > columns(Gp17 and Gp4). I want ascending order (1:25) of the index values. >order() -thomas
On 02/04/03 11:46, Mark G Orr wrote:>Hello, I'm somewhat new to R. I've searched the archive for the last year >and tried to consult the manual pages for the following problem, but did >not find an answer. > >I want to sort an array by the index values. Here is the arrayLooks like a matrix (which is, I guess, a type of array). To sort by the first column, try acc.gp.bl.wtmn[order(acc.gp.bl.wtmn[,1],]>>acc.gp.bl.wtmn > Gp17 Gp4 >1 0.5703125 0.6406250 >10 0.7812500 0.7109375 >11 0.8046875 0.7343750 >12 0.8359375 0.7890625 >13 0.8515625 0.7109375 >14 0.8281250 0.7343750 >15 0.8671875 0.7812500 >16 0.8125000 0.7578125 >17 0.7734375 0.7500000 >18 0.8000000 0.7800000 >19 0.8700000 0.7300000 >2 0.8046875 0.7265625 >20 0.8300000 0.7400000 >21 0.7500000 0.8000000 >22 0.7700000 0.7600000 >23 0.8200000 0.7800000 >24 0.7600000 0.7400000 >25 0.8100000 0.8000000 >3 0.7578125 0.7812500 >4 0.8203125 0.7890625 >5 0.7890625 0.7421875 >6 0.7265625 0.7812500 >7 0.8125000 0.7421875 >8 0.8515625 0.7265625 >9 0.8203125 0.6953125 > >My question is: How do I sort by the first index (the values are 1:25) >without losing the associated values per index value for the two >columns(Gp17 and Gp4). I want ascending order (1:25) of the index values. > >Thank you in advance. > >-Mark Orr > > > >Postdoctoral Fellow >Psychology Dept. >Carnegie Mellon Univ. >Pittsburgh, PA 15213 > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >http://www.stat.math.ethz.ch/mailman/listinfo/r-help-- Jonathan Baron, Professor of Psychology, University of Pennsylvania R page: http://finzi.psych.upenn.edu/
This should do it: > acc.gp.bl.wtmn[order(as.numeric(row.names(acc.gp.bl.wtmn))),] (This is not completely straightforward because in a dataframe, row names are almost always stored as character vectors, which means that you need to convert the row names to numeric values before calling order()) -- Tony Plate At Tuesday 11:46 AM 2/4/2003 -0500, Mark G Orr wrote:>Hello, I'm somewhat new to R. I've searched the archive for the last year >and tried to consult the manual pages for the following problem, but did >not find an answer. > >I want to sort an array by the index values. Here is the array > > >acc.gp.bl.wtmn > Gp17 Gp4 >1 0.5703125 0.6406250 >10 0.7812500 0.7109375 >11 0.8046875 0.7343750 >12 0.8359375 0.7890625 >13 0.8515625 0.7109375 >14 0.8281250 0.7343750 >15 0.8671875 0.7812500 >16 0.8125000 0.7578125 >17 0.7734375 0.7500000 >18 0.8000000 0.7800000 >19 0.8700000 0.7300000 >2 0.8046875 0.7265625 >20 0.8300000 0.7400000 >21 0.7500000 0.8000000 >22 0.7700000 0.7600000 >23 0.8200000 0.7800000 >24 0.7600000 0.7400000 >25 0.8100000 0.8000000 >3 0.7578125 0.7812500 >4 0.8203125 0.7890625 >5 0.7890625 0.7421875 >6 0.7265625 0.7812500 >7 0.8125000 0.7421875 >8 0.8515625 0.7265625 >9 0.8203125 0.6953125 > >My question is: How do I sort by the first index (the values are 1:25) >without losing the associated values per index value for the two >columns(Gp17 and Gp4). I want ascending order (1:25) of the index values. > >Thank you in advance. > >-Mark Orr > > > >Postdoctoral Fellow >Psychology Dept. >Carnegie Mellon Univ. >Pittsburgh, PA 15213 > >______________________________________________ >R-help at stat.math.ethz.ch mailing list >http://www.stat.math.ethz.ch/mailman/listinfo/r-help
Dear Mark, If I understand properly what you want, then one way to do it is acc.gp.bl.wtmn[order(as.numeric(row.names(acc.gp.bl.wtmn))),] (Note that the rows are currently ordered by character value.) I hope that this helps, John On Tue, 4 Feb 2003, Mark G Orr wrote:> Hello, I'm somewhat new to R. I've searched the archive for the last year > and tried to consult the manual pages for the following problem, but did > not find an answer. > > I want to sort an array by the index values. Here is the array > > >acc.gp.bl.wtmn > Gp17 Gp4 > 1 0.5703125 0.6406250 > 10 0.7812500 0.7109375 > 11 0.8046875 0.7343750 > 12 0.8359375 0.7890625 > 13 0.8515625 0.7109375 > 14 0.8281250 0.7343750 > 15 0.8671875 0.7812500 > 16 0.8125000 0.7578125 > 17 0.7734375 0.7500000 > 18 0.8000000 0.7800000 > 19 0.8700000 0.7300000 > 2 0.8046875 0.7265625 > 20 0.8300000 0.7400000 > 21 0.7500000 0.8000000 > 22 0.7700000 0.7600000 > 23 0.8200000 0.7800000 > 24 0.7600000 0.7400000 > 25 0.8100000 0.8000000 > 3 0.7578125 0.7812500 > 4 0.8203125 0.7890625 > 5 0.7890625 0.7421875 > 6 0.7265625 0.7812500 > 7 0.8125000 0.7421875 > 8 0.8515625 0.7265625 > 9 0.8203125 0.6953125 > > My question is: How do I sort by the first index (the values are 1:25) > without losing the associated values per index value for the two > columns(Gp17 and Gp4). I want ascending order (1:25) of the index values. > > Thank you in advance. > > -Mark Orr > > > > Postdoctoral Fellow > Psychology Dept. > Carnegie Mellon Univ. > Pittsburgh, PA 15213 > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > http://www.stat.math.ethz.ch/mailman/listinfo/r-help > >