R : Copyright 2006, The R Foundation for Statistical Computing Version 2.3.1 (2006-06-01) ISBN 3-900051-07-0 R is free software and comes with ABSOLUTELY NO WARRANTY. You are welcome to redistribute it under certain conditions. Type 'license()' or 'licence()' for distribution details. R is a collaborative project with many contributors. Type 'contributors()' for more information and 'citation()' on how to cite R or R packages in publications. Type 'demo()' for some demos, 'help()' for on-line help, or 'help.start()' for an HTML browser interface to help. Type 'q()' to quit R. > # Goal: To do sorting. > # > # The approach here needs to be explained. If `i' is a vector of > # integers, then the data frame D[i,] picks up rows from D based > # on the values found in `i'. > # > # The order() function makes an integer vector which is a correct > # ordering for the purpose of sorting. > > D <- data.frame(x=c(1,2,3,1), y=c(7,19,2,2)) > D x y 1 1 7 2 2 19 3 3 2 4 1 2 > > # Sort on x > indexes <- order(D$x) > D[indexes,] x y 1 1 7 4 1 2 2 2 19 3 3 2 > > # Print out sorted dataset, sorted in reverse by y > D[rev(order(D$y)),] x y 2 2 19 1 1 7 4 1 2 3 3 2 >